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

git pushed a commit to branch master
in repository efl.

View the commit online.

commit 623902284c3c059226e635b9d82fbaf98a23dd02
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 21 11:51:47 2026 -0600

    evas:textgrid: draw cell backgrounds from a one-pixel-per-cell image
    
    The background path merged neighbouring cells only when their colors
    were equal. On a screen with many colors that is about one rect draw per
    cell, each with its own context color and cutout setup. When the whole
    object is redrawn every frame, as with GL here, a 200x60 grid costs
    about 12000 rect draws even when two rows changed.
    
    The textgrid now keeps a cols x rows premultiplied ARGB buffer and draws
    it in one nearest-scaled image_draw. Only changed rows are rewritten and
    marked dirty. Run-merged rects are kept as the fallback when the image
    cannot be created, for render ops other than BLEND (alpha-0 cells must
    stay untouched), and when a frame needs few rects: below 3 runs per row
    on average it switches to rects, above 6 to the image. A typical
    terminal screen measured about 1.5 runs per row and stays on rects,
    where the image lost in software. The engine image-unset hook lets
    backends that drop engine images (wayland_egl) make the textgrid redraw
    fully instead of keeping a stale image.
    
    Two visible changes:
    - All backgrounds are drawn before any text, not row by row. A glyph
      that overshoots into the next row (italic, descender) is no longer
      painted over by that row's background, as in other terminals.
      Terminology was checked and does not rely on the old order.
    - On GL, image backgrounds pass through the shader's 2x2 ordered
      dither, so a flat color can gain one level on one pixel in four. It is
      constant from frame to frame and does not accumulate, because the
      image is opaque and fully redrawn. Rect backgrounds are exact.
    
    Numbers, median fps, without -> with this change:
      GL:           Truecolor Fire    309 -> 413 (+34%)
                    Truecolor Partial 396 -> 825 (+108%)
                    Textgrid Change   866 -> 984 (+14%)
      buffer:       Partial 539 -> 585 (+9%)
                    Textgrid Change 1473 -> 1359 (-7.7%, within the ranges
                    but consistently lower)
      software_x11: Partial 645 -> 704 (+9%)
    The other tests are flat.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/evas/canvas/evas_main.c            |   2 +
 src/lib/evas/canvas/evas_object_textgrid.c | 223 +++++++++++---
 src/lib/evas/include/evas_private.h        |   1 +
 src/tests/evas/evas_test_textgrid.c        | 466 ++++++++++++++++++++++++++++-
 4 files changed, 648 insertions(+), 44 deletions(-)

diff --git a/src/lib/evas/canvas/evas_main.c b/src/lib/evas/canvas/evas_main.c
index f28c2fff07..7ee0c1696f 100644
--- a/src/lib/evas/canvas/evas_main.c
+++ b/src/lib/evas/canvas/evas_main.c
@@ -1110,6 +1110,8 @@ _image_data_unset(Evas_Object_Protected_Data *obj, Eina_List **list)
         data->engine_data =
           obj->layer->evas->engine.func->polygon_points_clear(ENC,
                                                               data->engine_data))
+   else if (efl_isa(obj->object, EVAS_TEXTGRID_CLASS))
+     evas_object_textgrid_engine_image_unset(obj->object);
    else return;
 #undef CHECK
    evas_object_ref(obj->object);
diff --git a/src/lib/evas/canvas/evas_object_textgrid.c b/src/lib/evas/canvas/evas_object_textgrid.c
index 3b1c2eff12..26a0aad3d9 100644
--- a/src/lib/evas/canvas/evas_object_textgrid.c
+++ b/src/lib/evas/canvas/evas_object_textgrid.c
@@ -93,6 +93,21 @@ struct _Evas_Textgrid_Data
 
    Evas_Textgrid_Liga            *liga; /* NULL when ligatures are off */
 
+   /* Cell backgrounds: one premultiplied ARGB pixel per cell, drawn as a
+    * single image scaled up to the grid with nearest filtering.  The
+    * pixels outlive the engine image, which wraps them without a copy
+    * and is recreated from them when the engine drops it.  Where the
+    * image cannot do the job (see _textgrid_bg_rects_wanted()) the
+    * backgrounds are drawn as rects instead, one per run of cells of
+    * the same color. */
+   struct {
+      DATA32                     *pixels;
+      void                       *image;
+      Eina_Bool                   rects;
+      Eina_Bool                   image_failed;
+      int                         runs; /* sum of the rows' bg_runs */
+   } bg;
+
    unsigned int                   changed : 1;
    unsigned int                   core_change : 1;
    unsigned int                   row_change : 1;
@@ -113,9 +128,10 @@ struct _Evas_Textgrid_Data
 struct _Evas_Object_Textgrid_Row
 {
    int ch1, ch2; // change region, -1 == none
+   int bg_runs; // runs of same-colored backgrounds, what rects would take
    int rects_num, texts_num, lines_num;
    int rects_alloc, texts_alloc, lines_alloc;
-   Evas_Object_Textgrid_Rect *rects; // rects + colors
+   Evas_Object_Textgrid_Rect *rects; // only filled when bg is drawn as rects
    Evas_Object_Textgrid_Text *texts; // text
    Evas_Object_Textgrid_Line *lines; // underlines, strikethroughs, overlines
 };
@@ -405,6 +421,7 @@ evas_object_textgrid_init(Evas_Object *eo_obj)
    memset(o->palette_standard, 0, sizeof(o->palette_standard));
    memset(o->palette_extended, 0, sizeof(o->palette_extended));
    o->text_props_cache = _textgrid_cache_new();
+   o->bg.rects = EINA_TRUE;
 }
 
 /* Reset a row for reuse without freeing its backing arrays.
@@ -496,11 +513,39 @@ evas_object_textgrid_rows_reset(Evas_Object *eo_obj)
 }
 
 static void
-evas_object_textgrid_free(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj EINA_UNUSED)
+_textgrid_bg_image_free(Evas_Object_Protected_Data *obj, Evas_Textgrid_Data *o)
+{
+   o->bg.image_failed = EINA_FALSE;
+   if (!o->bg.image) return;
+   if ((obj->layer) && (obj->layer->evas) && (ENC))
+     ENFN->image_free(ENC, o->bg.image);
+   o->bg.image = NULL;
+}
+
+/* Called when the engine is about to drop its images: the pixels are
+ * kept, the next render wraps them in a new image.  What the engine
+ * drew from the old one is gone with its surface, so redraw it all. */
+void
+evas_object_textgrid_engine_image_unset(Evas_Object *eo_obj)
+{
+   Evas_Object_Protected_Data *obj = efl_data_scope_get(eo_obj, EFL_CANVAS_OBJECT_CLASS);
+   Evas_Textgrid_Data *o = efl_data_scope_get(eo_obj, MY_CLASS);
+
+   _textgrid_bg_image_free(obj, o);
+   o->changed = 1;
+   o->pal_change = 1;
+   evas_object_change(eo_obj, obj);
+}
+
+static void
+evas_object_textgrid_free(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj)
 {
    Evas_Textgrid_Data *o = efl_data_scope_get(eo_obj, MY_CLASS);
 
    /* free obj */
+   _textgrid_bg_image_free(obj, o);
+   free(o->bg.pixels);
+   o->bg.pixels = NULL;
    evas_object_textgrid_rows_clear(eo_obj);
    if (o->cur.rows) free(o->cur.rows);
    if (o->cur.font_name) eina_stringshare_del(o->cur.font_name);
@@ -690,7 +735,6 @@ evas_object_textgrid_row_text_append(Evas_Object_Textgrid_Row *row,
           }
         if (cached)
           {
-             /* Cache hit: copy the cached props and take an independent ref */
              /* Cache hit: copy the cached props and take an independent ref.
               * The glyph array is shared too, so a cell costs no allocation
               * until its glyphs change. */
@@ -839,6 +883,39 @@ _drop_glyphs_ref(const void *container EINA_UNUSED, void *data, void *fdata)
    return EINA_TRUE;
 }
 
+/* Premultiplied ARGB of a cell background; alpha 0 means no color. */
+static inline DATA32
+_textgrid_bg_pixel(const Evas_Object_Textgrid_Color *c)
+{
+   if (c->a == 0) return 0;
+   return ((DATA32)c->a << 24) | ((DATA32)c->r << 16) |
+          ((DATA32)c->g << 8) | c->b;
+}
+
+/* A scaled image costs per pixel of the area redrawn, rects cost per rect:
+ * a grid whose rows hold few runs of background color is cheaper as rects,
+ * a grid whose rows hold many is cheaper as the image.  The grid changes
+ * form when the average number of runs per row goes below the first limit
+ * (to rects) or above the second (to the image).  Between the two it keeps
+ * its form, so that a grid hovering around one limit does not switch, and
+ * regenerate every row, each frame. */
+#define TEXTGRID_BG_RECTS_BELOW_RUNS_PER_ROW 3
+#define TEXTGRID_BG_IMAGE_ABOVE_RUNS_PER_ROW 6
+
+/* The image cannot be used when the engine cannot make one, and with a
+ * render op other than BLEND, where a pixel of alpha 0 would be copied
+ * (or subtracted) instead of leaving the canvas alone. */
+static inline Eina_Bool
+_textgrid_bg_rects_wanted(Evas_Textgrid_Data *o,
+                          Evas_Object_Protected_Data *obj)
+{
+   if ((o->bg.image_failed) || (obj->cur->render_op != EVAS_RENDER_BLEND))
+     return EINA_TRUE;
+   if (o->bg.rects)
+     return o->bg.runs <= (TEXTGRID_BG_IMAGE_ABOVE_RUNS_PER_ROW * o->cur.h);
+   return o->bg.runs < (TEXTGRID_BG_RECTS_BELOW_RUNS_PER_ROW * o->cur.h);
+}
+
 /* Append the resolved background color of one cell to the run of
  * same-colored rectangles being built for the row.  The run state is
  * passed by pointer and kept by the caller across cells. */
@@ -935,7 +1012,7 @@ _textgrid_render_cell_text(Evas_Object_Textgrid_Row *row,
    if (c)
      {
         /* XXX: underlines, strikethroughs and overlines don't get merged
-         * into horizontal runs like bg rects above */
+         * into horizontal runs */
         if (cells->underline)
           evas_object_textgrid_row_line_append(row, xp, w,
                                                underline_y, line_th,
@@ -951,7 +1028,7 @@ _textgrid_render_cell_text(Evas_Object_Textgrid_Row *row,
 }
 
 static void
-evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
+evas_object_textgrid_render(Evas_Object *eo_obj,
                             Evas_Object_Protected_Data *obj,
                             void *type_private_data,
                             void *engine, void *output, void *context, void *surface,
@@ -961,11 +1038,14 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
    const Evas_Textgrid_Truecolor *tc;
    const Evas_Object_Textgrid_Color *c;
    Evas_Object_Textgrid_Color rc;
+   DATA32 *px = NULL;
    int xx, yy, xp, yp, w, h, ww, hh;
-   int rx = 0, rw = 0, run;
+   int rx = 0, rw = 0, run, nruns;
+   DATA32 prev;
+   int bg_x1 = INT_MAX, bg_x2 = -1, bg_y1 = INT_MAX, bg_y2 = -1;
    int line_th, underline_y, strikethrough_y;
    Evas_Textgrid_Liga_Run lrun;
-   Eina_Bool liga_on, multi_font;
+   Eina_Bool liga_on, rects_wanted, multi_font;
    Evas_Font_Array sync_texts = { NULL, 0 };
 
    /* render object to surface with context, and offset by x,y */
@@ -975,6 +1055,39 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
 
    if (!(o->font_normal) || (!o->cur.cells)) return;
 
+   rects_wanted = _textgrid_bg_rects_wanted(o, obj);
+   if (!rects_wanted)
+     {
+        if (!o->bg.pixels)
+          o->bg.pixels = calloc(o->cur.w * o->cur.h, sizeof(DATA32));
+        if ((o->bg.pixels) && (!o->bg.image))
+          {
+             o->bg.image = ENFN->image_new_from_data(engine, o->cur.w,
+                                                     o->cur.h, o->bg.pixels,
+                                                     EINA_TRUE,
+                                                     EVAS_COLORSPACE_ARGB8888);
+             /* Dirty from the start, so that the cache never clones the
+              * pixels into a copy that later writes would not reach. */
+             if (o->bg.image)
+               o->bg.image = ENFN->image_dirty_region(engine, o->bg.image,
+                                                      0, 0, o->cur.w,
+                                                      o->cur.h);
+          }
+        if ((!o->bg.pixels) || (!o->bg.image))
+          {
+             ERR("No image for the textgrid backgrounds, using rects.");
+             o->bg.image_failed = EINA_TRUE;
+             rects_wanted = EINA_TRUE;
+          }
+     }
+   if (rects_wanted != o->bg.rects)
+     {
+        /* Rows only hold the backgrounds of the form used when they
+         * were generated */
+        o->bg.rects = rects_wanted;
+        evas_object_textgrid_rows_reset(eo_obj);
+     }
+
    w = o->cur.char_width;
    h = o->cur.char_height;
    ww = obj->cur->geometry.w;
@@ -1001,14 +1114,19 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
 
         if (row->ch1 >= 0)
           {
+             if (row->ch1 < bg_x1) bg_x1 = row->ch1;
+             if (row->ch2 > bg_x2) bg_x2 = row->ch2;
+             if (yy < bg_y1) bg_y1 = yy;
+             bg_y2 = yy;
              row->ch1 = -1;
              row->ch2 = 0;
-             run = 0;
              lrun.len = 0;
              xp = 0;
-             /* Run state as one 4-byte color: comparing and copying it is
-              * a single load and store in the per-cell loop. */
+             run = 0;
+             nruns = 0;
+             prev = 0;
              rc.r = rc.g = rc.b = rc.a = 0;
+             if (!rects_wanted) px = o->bg.pixels + (yy * o->cur.w);
              if (tc)
                {
                   const Evas_Textgrid_Truecolor *t = tc;
@@ -1020,8 +1138,16 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
                        else
                          c = _textgrid_palette_color_get(o, cells->bg_extended,
                                                          cells->bg);
-                       _textgrid_render_cell_bg(row, &run, &rc, &rx, &rw,
-                                                c, xp, w);
+                       if (px)
+                         {
+                            DATA32 pix = _textgrid_bg_pixel(c);
+
+                            if ((pix) && (pix != prev)) nruns++;
+                            prev = pix;
+                            *px++ = pix;
+                         }
+                       else _textgrid_render_cell_bg(row, &run, &rc, &rx, &rw,
+                                                     c, xp, w);
                        c = NULL;
                        if ((cells->codepoint > 0) || (cells->underline) ||
                            (cells->strikethrough) || (cells->overline))
@@ -1047,8 +1173,16 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
                     {
                        c = _textgrid_palette_color_get(o, cells->bg_extended,
                                                        cells->bg);
-                       _textgrid_render_cell_bg(row, &run, &rc, &rx, &rw,
-                                                c, xp, w);
+                       if (px)
+                         {
+                            DATA32 pix = _textgrid_bg_pixel(c);
+
+                            if ((pix) && (pix != prev)) nruns++;
+                            prev = pix;
+                            *px++ = pix;
+                         }
+                       else _textgrid_render_cell_bg(row, &run, &rc, &rx, &rw,
+                                                     c, xp, w);
                        c = NULL;
                        if ((cells->codepoint > 0) || (cells->underline) ||
                            (cells->strikethrough) || (cells->overline))
@@ -1072,11 +1206,39 @@ evas_object_textgrid_render(Evas_Object *eo_obj EINA_UNUSED,
                   evas_object_textgrid_row_rect_append(row, rx, rw,
                                                        rc.r, rc.g, rc.b, rc.a);
                }
+             if (rects_wanted) nruns = row->rects_num;
+             o->bg.runs += nruns - row->bg_runs;
+             row->bg_runs = nruns;
           }
         else cells += o->cur.w; /* skipped row: cells move as a block */
         if (tc) tc += o->cur.w;
      }
+
+   if ((!rects_wanted) && (bg_y2 >= 0))
+     o->bg.image = ENFN->image_dirty_region(engine, o->bg.image,
+                                            bg_x1, bg_y1,
+                                            bg_x2 - bg_x1 + 1,
+                                            bg_y2 - bg_y1 + 1);
+
    yp = obj->cur->geometry.y + y;
+   if (!rects_wanted)
+     {
+        Eina_Bool async_unref;
+
+        xp = obj->cur->geometry.x + x;
+        ENFN->context_cutout_target(engine, context, xp, yp,
+                                    o->cur.w * w, o->cur.h * h);
+        async_unref = ENFN->image_draw(engine, output, context, surface,
+                                       o->bg.image,
+                                       0, 0, o->cur.w, o->cur.h,
+                                       xp, yp, o->cur.w * w, o->cur.h * h,
+                                       EINA_FALSE, do_async);
+        if ((do_async) && (async_unref))
+          {
+             evas_cache_image_ref((Image_Entry *)o->bg.image);
+             evas_unref_queue_image_put(obj->layer->evas, o->bg.image);
+          }
+     }
    multi_font = !!ENFN->multi_font_draw;
    if ((multi_font) && (!do_async))
      {
@@ -1507,6 +1669,13 @@ _evas_textgrid_grid_size_set(Eo *eo_obj, Evas_Textgrid_Data *o, int w, int h)
 
    evas_object_async_block(obj);
    evas_object_textgrid_rows_clear(eo_obj);
+   _textgrid_bg_image_free(obj, o);
+   free(o->bg.pixels);
+   o->bg.pixels = NULL;
+   /* Start from a run count that keeps the current form: the rows are
+    * regenerated anyway, and counted again, by the next render. */
+   o->bg.runs = o->bg.rects ? 0 :
+     TEXTGRID_BG_RECTS_BELOW_RUNS_PER_ROW * h;
    if (o->cur.rows)
      {
         free(o->cur.rows);
@@ -1549,7 +1718,6 @@ _evas_textgrid_grid_size_set(Eo *eo_obj, Evas_Textgrid_Data *o, int w, int h)
              for (j = 0; j < i; j++)
                {
                   free(o->cur.rows[j].texts);
-                  free(o->cur.rows[j].rects);
                   free(o->cur.rows[j].lines);
                }
              free(o->cur.rows);
@@ -1560,27 +1728,6 @@ _evas_textgrid_grid_size_set(Eo *eo_obj, Evas_Textgrid_Data *o, int w, int h)
           }
         r->texts_alloc = w;
 
-        r->rects = calloc(w, sizeof(Evas_Object_Textgrid_Rect));
-        if (!r->rects)
-          {
-             int j;
-             free(r->texts);
-             r->texts = NULL;
-             r->texts_alloc = 0;
-             for (j = 0; j < i; j++)
-               {
-                  free(o->cur.rows[j].texts);
-                  free(o->cur.rows[j].rects);
-                  free(o->cur.rows[j].lines);
-               }
-             free(o->cur.rows);
-             o->cur.rows = NULL;
-             free(o->cur.cells);
-             o->cur.cells = NULL;
-             return;
-          }
-        r->rects_alloc = w;
-
         r->lines = calloc(8, sizeof(Evas_Object_Textgrid_Line));
         if (!r->lines)
           {
@@ -1588,13 +1735,9 @@ _evas_textgrid_grid_size_set(Eo *eo_obj, Evas_Textgrid_Data *o, int w, int h)
              free(r->texts);
              r->texts = NULL;
              r->texts_alloc = 0;
-             free(r->rects);
-             r->rects = NULL;
-             r->rects_alloc = 0;
              for (j = 0; j < i; j++)
                {
                   free(o->cur.rows[j].texts);
-                  free(o->cur.rows[j].rects);
                   free(o->cur.rows[j].lines);
                }
              free(o->cur.rows);
diff --git a/src/lib/evas/include/evas_private.h b/src/lib/evas/include/evas_private.h
index df6cd01a7d..9fedeb9348 100644
--- a/src/lib/evas/include/evas_private.h
+++ b/src/lib/evas/include/evas_private.h
@@ -1374,6 +1374,7 @@ EVAS_API const char *_evas_module_libdir_get(void);
 const char *_evas_module_datadir_get(void);
 EVAS_API Eina_List *_evas_canvas_image_data_unset(Evas *eo_e);
 EVAS_API void _evas_canvas_image_data_regenerate(Eina_List *list);
+void evas_object_textgrid_engine_image_unset(Evas_Object *eo_obj);
 void _evas_image_preload_update(Eo *eo_obj, Eina_File *f);
 Eina_Bool evas_render_mapped(Evas_Public_Data *e, Evas_Object *obj,
                              Evas_Object_Protected_Data *source_pd,
diff --git a/src/tests/evas/evas_test_textgrid.c b/src/tests/evas/evas_test_textgrid.c
index 07fa5d84d2..f973b4d96e 100644
--- a/src/tests/evas/evas_test_textgrid.c
+++ b/src/tests/evas/evas_test_textgrid.c
@@ -9,6 +9,8 @@
 #include <string.h>
 
 #include "evas_suite.h"
+#include "evas_common_private.h"
+#include "evas_private.h"
 
 #define TEST_FONT_NAME "DejaVuSansMono"
 #define TEST_FONT_SOURCE TESTS_SRC_DIR "/fonts/TestFontMono.eet"
@@ -63,21 +65,28 @@ _backdrop_add(Evas *e)
 }
 
 static Evas_Object *
-_textgrid_new(Evas *e)
+_textgrid_sized_new(Evas *e, int font_size, int gw, int gh)
 {
    Evas_Object *tg = evas_object_textgrid_add(e);
 
    fail_if(!tg);
    evas_object_textgrid_font_source_set(tg, TEST_FONT_SOURCE);
-   evas_object_textgrid_font_set(tg, TEST_FONT_NAME, 16);
-   evas_object_textgrid_size_set(tg, GRID_W, GRID_H);
+   evas_object_textgrid_font_set(tg, TEST_FONT_NAME, font_size);
+   evas_object_textgrid_size_set(tg, gw, gh);
    evas_object_textgrid_cell_size_get(tg, &cw, &ch);
    fail_if((cw <= 0) || (ch <= 0));
-   evas_object_geometry_set(tg, 0, 0, cw * GRID_W, ch * GRID_H);
+   fail_if((cw * gw > CANVAS_W) || (ch * gh > CANVAS_H));
+   evas_object_geometry_set(tg, 0, 0, cw * gw, ch * gh);
    evas_object_show(tg);
    return tg;
 }
 
+static Evas_Object *
+_textgrid_new(Evas *e)
+{
+   return _textgrid_sized_new(e, 16, GRID_W, GRID_H);
+}
+
 static void
 _palette_std_set(Evas_Object *tg, int idx, unsigned int argb)
 {
@@ -394,6 +403,366 @@ EFL_START_TEST(evas_textgrid_test_truecolor_cellrow_set)
 }
 EFL_END_TEST
 
+/* Background pixel tests.  A model holds the premultiplied ARGB every
+ * cell is expected to show, and the whole canvas is compared against it,
+ * pixel by pixel, so that a cell edge landing one pixel off is caught. */
+#define MODEL_W 16
+#define MODEL_H 8
+
+typedef unsigned int Model[MODEL_H][MODEL_W];
+
+static unsigned int
+_model_color(int x, int y)
+{
+   return ARGB(255, 10 + x * 13, 20 + y * 29, (x * 37 + y * 91) & 0xff);
+}
+
+static void
+_model_fill(Model model, int gw, int gh)
+{
+   int x, y;
+
+   for (y = 0; y < gh; y++)
+     for (x = 0; x < gw; x++)
+       model[y][x] = _model_color(x, y);
+}
+
+/* Write the model colors of cells [x1, x2] x [y1, y2] as truecolor
+ * backgrounds, and flag them as changed. */
+static void
+_model_apply(Evas_Object *tg, Model model, int x1, int y1, int x2, int y2)
+{
+   int x, y;
+
+   for (y = y1; y <= y2; y++)
+     {
+        Evas_Textgrid_Truecolor *tcs;
+        Evas_Textgrid_Cell *cells;
+
+        tcs = evas_object_textgrid_truecolor_cellrow_get(tg, y);
+        cells = evas_object_textgrid_cellrow_get(tg, y);
+        fail_if((!tcs) || (!cells));
+        for (x = x1; x <= x2; x++)
+          {
+             unsigned int c = model[y][x];
+
+             tcs[x].bg.a = c >> 24;
+             tcs[x].bg.r = (c >> 16) & 0xff;
+             tcs[x].bg.g = (c >> 8) & 0xff;
+             tcs[x].bg.b = c & 0xff;
+             cells[x].bg_truecolor = 1;
+          }
+     }
+   evas_object_textgrid_update_add(tg, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
+}
+
+/* What a cell of model color 0 lets through, when something sits under
+ * the grid */
+static unsigned int _model_under = COL_CANVAS;
+
+/* Every canvas pixel must show the model color of the cell it falls in,
+ * or the bare canvas outside the grid and outside the clip, if any. */
+static void
+_model_check(const unsigned int *pix, Model model, int gw, int gh,
+             const Eina_Rectangle *clip)
+{
+   int px, py;
+
+   for (py = 0; py < CANVAS_H; py++)
+     for (px = 0; px < CANVAS_W; px++)
+       {
+          unsigned int expected = COL_CANVAS;
+          unsigned int got = pix[py * CANVAS_W + px];
+
+          if ((px < cw * gw) && (py < ch * gh) &&
+              ((!clip) || ((px >= clip->x) && (px < clip->x + clip->w) &&
+                           (py >= clip->y) && (py < clip->y + clip->h))))
+            {
+               expected = model[py / ch][px / cw];
+               if (!expected) expected = _model_under;
+            }
+          if (got != expected)
+            ck_abort_msg("pixel (%d,%d) in cell (%d,%d) of %dx%d px got "
+                         "%#08x expected %#08x", px, py, px / cw, py / ch,
+                         cw, ch, got, expected);
+       }
+}
+
+static void
+_model_snapshot_check(Ecore_Evas *ee, Model model, int gw, int gh)
+{
+   unsigned int *pix = _snapshot(ee);
+
+   _model_check(pix, model, gw, gh, NULL);
+   free(pix);
+}
+
+/* Each cell shows its own color over its whole rectangle, whatever the
+ * cell size */
+EFL_START_TEST(evas_textgrid_test_bg_cells_exact)
+{
+   static const int sizes[] = { 9, 16, 23 };
+   unsigned int i;
+
+   for (i = 0; i < EINA_C_ARRAY_LENGTH(sizes); i++)
+     {
+        START_TG_TEST();
+        Evas_Object *tg = _textgrid_sized_new(e, sizes[i], MODEL_W - 4,
+                                              MODEL_H - 2);
+        Model model;
+
+        evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+        _model_fill(model, MODEL_W - 4, MODEL_H - 2);
+        _model_apply(tg, model, 0, 0, MODEL_W - 5, MODEL_H - 3);
+        _model_snapshot_check(ee, model, MODEL_W - 4, MODEL_H - 2);
+        END_TG_TEST();
+     }
+}
+EFL_END_TEST
+
+/* A frame after changing one row updates that row, and only that row;
+ * then the same for a few cells in the middle of a row */
+EFL_START_TEST(evas_textgrid_test_bg_partial_update)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+   int x;
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   _model_fill(model, GRID_W, GRID_H);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   for (x = 0; x < GRID_W; x++)
+     model[2][x] = ARGB(255, 250 - x, x * 3, 17);
+   _model_apply(tg, model, 0, 2, GRID_W - 1, 2);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   model[0][3] = COL_BLUE;
+   model[0][4] = COL_GREEN;
+   _model_apply(tg, model, 3, 0, 4, 0);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   /* first and last cells of the grid */
+   model[0][0] = COL_GREEN;
+   model[GRID_H - 1][GRID_W - 1] = COL_RED;
+   _model_apply(tg, model, 0, 0, 0, 0);
+   _model_apply(tg, model, GRID_W - 1, GRID_H - 1, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   /* nothing changed: the frame is the same */
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* Alpha 0 stays transparent, also for a cell that had a color before;
+ * a partially transparent color is drawn premultiplied */
+EFL_START_TEST(evas_textgrid_test_bg_transparent)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   _model_fill(model, GRID_W, GRID_H);
+   model[1][1] = 0;
+   model[1][2] = ARGB(128, 64, 0, 32);
+   model[3][7] = 0;
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   model[0][0] = 0;
+   model[1][2] = 0;
+   model[3][0] = ARGB(128, 0, 128, 0);
+   _model_apply(tg, model, 0, 0, 2, 3);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* A resized grid starts clear, and is correct when smaller and larger */
+EFL_START_TEST(evas_textgrid_test_bg_resize)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+   int x, y, gw, gh;
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   _model_fill(model, GRID_W, GRID_H);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   gw = 4;
+   gh = 2;
+   evas_object_textgrid_size_set(tg, gw, gh);
+   evas_object_geometry_set(tg, 0, 0, cw * gw, ch * gh);
+   memset(model, 0, sizeof(model));
+   _model_snapshot_check(ee, model, gw, gh);
+   _model_fill(model, gw, gh);
+   _model_apply(tg, model, 0, 0, gw - 1, gh - 1);
+   _model_snapshot_check(ee, model, gw, gh);
+
+   gw = 12;
+   gh = 6;
+   evas_object_textgrid_size_set(tg, gw, gh);
+   evas_object_geometry_set(tg, 0, 0, cw * gw, ch * gh);
+   for (y = 0; y < gh; y++)
+     for (x = 0; x < gw; x++)
+       model[y][x] = _model_color(gw - x, gh - y);
+   _model_apply(tg, model, 0, 0, gw - 1, gh - 1);
+   _model_snapshot_check(ee, model, gw, gh);
+
+   /* and a change of one row afterwards */
+   for (x = 0; x < gw; x++)
+     model[5][x] = COL_BLUE;
+   _model_apply(tg, model, 0, 5, gw - 1, 5);
+   _model_snapshot_check(ee, model, gw, gh);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* A palette entry change redraws every cell using it, with no cell
+ * touched */
+EFL_START_TEST(evas_textgrid_test_bg_palette_change)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+   int x, y;
+
+   _palette_std_set(tg, 0, COL_RED);
+   _palette_std_set(tg, 1, COL_BLUE);
+   for (y = 0; y < GRID_H; y++)
+     for (x = 0; x < GRID_W; x++)
+       {
+          evas_object_textgrid_cellrow_get(tg, y)[x].bg = (x + y) & 1;
+          model[y][x] = ((x + y) & 1) ? COL_BLUE : COL_RED;
+       }
+   evas_object_textgrid_update_add(tg, 0, 0, GRID_W, GRID_H);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   _palette_std_set(tg, 1, COL_GREEN);
+   for (y = 0; y < GRID_H; y++)
+     for (x = 0; x < GRID_W; x++)
+       if ((x + y) & 1) model[y][x] = COL_GREEN;
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* The grid is clipped through the image like any other object */
+EFL_START_TEST(evas_textgrid_test_bg_clipped)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Evas_Object *clipper = evas_object_rectangle_add(e);
+   Eina_Rectangle clip = { cw + 3, ch + 5, 3 * cw + 1, 2 * ch - 2 };
+   Model model;
+   unsigned int *pix;
+
+   evas_object_geometry_set(clipper, clip.x, clip.y, clip.w, clip.h);
+   evas_object_show(clipper);
+   evas_object_clip_set(tg, clipper);
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   _model_fill(model, GRID_W, GRID_H);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   pix = _snapshot(ee);
+   _model_check(pix, model, GRID_W, GRID_H, &clip);
+   free(pix);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* The engine can drop its images while the grid lives on, as the
+ * wayland backends do when they switch surfaces */
+
+EFL_START_TEST(evas_textgrid_test_bg_engine_image_dropped)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+   unsigned int *pix;
+   int i;
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   _model_fill(model, GRID_W, GRID_H);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   /* Wipe what the canvas shows: only a redraw can bring it back */
+   pix = (unsigned int *)ecore_evas_buffer_pixels_get(ee);
+   for (i = 0; i < CANVAS_W * CANVAS_H; i++) pix[i] = 0x55aa55aa;
+   _evas_canvas_image_data_regenerate(_evas_canvas_image_data_unset(e));
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   model[1][1] = COL_RED;
+   _model_apply(tg, model, 1, 1, 1, 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
+/* With MUL a cell of alpha 0 must leave the canvas alone: a transparent
+ * pixel would zero it.  White cells multiply to no change.  Then the
+ * switch back to BLEND redraws everything. */
+EFL_START_TEST(evas_textgrid_test_bg_render_op_mul)
+{
+   START_TG_TEST();
+   Evas_Object *under = evas_object_rectangle_add(e);
+   Evas_Object *tg;
+   Model model;
+   int x, y;
+
+   tg = _textgrid_new(e);
+   evas_object_color_set(under, 0x11, 0x22, 0x33, 255);
+   evas_object_geometry_set(under, 0, 0, cw * GRID_W, ch * GRID_H);
+   evas_object_stack_below(under, tg);
+   evas_object_show(under);
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+   for (y = 0; y < GRID_H; y++)
+     for (x = 0; x < GRID_W; x++)
+       model[y][x] = ((x + y) % 3) ? ARGB(255, 255, 255, 255) : 0;
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+
+   _model_under = ARGB(255, 0x11, 0x22, 0x33);
+   evas_object_render_op_set(tg, EVAS_RENDER_MUL);
+   {
+      unsigned int *pix = _snapshot(ee);
+
+      for (y = 0; y < GRID_H; y++)
+        for (x = 0; x < GRID_W; x++)
+          model[y][x] = _model_under;
+      _model_check(pix, model, GRID_W, GRID_H, NULL);
+      free(pix);
+   }
+
+   evas_object_render_op_set(tg, EVAS_RENDER_BLEND);
+   _model_fill(model, GRID_W, GRID_H);
+   model[1][2] = 0;
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   model[1][2] = _model_under;
+   {
+      unsigned int *pix = _snapshot(ee);
+
+      _model_check(pix, model, GRID_W, GRID_H, NULL);
+      free(pix);
+   }
+   _model_under = COL_CANVAS;
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
 static void
 _text_cells_fill(Evas_Object *tg, int only)
 {
@@ -515,6 +884,86 @@ EFL_START_TEST(evas_textgrid_test_text_cells_independent)
 }
 EFL_END_TEST
 
+/* What a grid of the given size shows, one color for all its cells when
+ * few runs are wanted, one per cell when many are */
+static void
+_bg_model_fill(Model model, int gw, int gh, Eina_Bool many)
+{
+   int x, y;
+
+   for (y = 0; y < gh; y++)
+     for (x = 0; x < gw; x++)
+       model[y][x] = many ? _model_color(x, y) : COL_RED;
+}
+
+static void
+_textgrid_grid_resize(Evas_Object *tg, int gw, int gh)
+{
+   evas_object_textgrid_size_set(tg, gw, gh);
+   evas_object_resize(tg, cw * gw, ch * gh);
+}
+
+/* The backgrounds are drawn as rects, or as an image, according to how
+ * many runs of color the rows hold.  Whichever it is, and whenever it
+ * changes, the pixels must be those of the model: when going through
+ * partial updates, around the limits, and across a resize. */
+EFL_START_TEST(evas_textgrid_test_bg_form_switch)
+{
+   START_TG_TEST();
+   Evas_Object *tg = _textgrid_new(e);
+   Model model;
+   int x, y, i;
+
+   evas_object_textgrid_truecolor_set(tg, EINA_TRUE);
+
+   /* one run per row: rects */
+   _bg_model_fill(model, GRID_W, GRID_H, EINA_FALSE);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   /* a run per cell: the image */
+   _bg_model_fill(model, GRID_W, GRID_H, EINA_TRUE);
+   _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+   _model_snapshot_check(ee, model, GRID_W, GRID_H);
+
+   /* between the limits, one row at a time, in both directions */
+   for (i = 0; i < 2; i++)
+     {
+        for (y = 0; y < GRID_H; y++)
+          {
+             for (x = 0; x < GRID_W; x++)
+               model[y][x] = (x < 4 + i * 2) ? _model_color(x, y) : COL_BLUE;
+             _model_apply(tg, model, 0, y, GRID_W - 1, y);
+             _model_snapshot_check(ee, model, GRID_W, GRID_H);
+          }
+        _bg_model_fill(model, GRID_W, GRID_H, EINA_FALSE);
+        _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+        _model_snapshot_check(ee, model, GRID_W, GRID_H);
+        _bg_model_fill(model, GRID_W, GRID_H, EINA_TRUE);
+        _model_apply(tg, model, 0, 0, GRID_W - 1, GRID_H - 1);
+        _model_snapshot_check(ee, model, GRID_W, GRID_H);
+     }
+
+   /* a resize keeps working in either form, and with the same cells
+    * as before it has to show them again once they are set again */
+   for (i = 0; i < 4; i++)
+     {
+        int gw = (i & 1) ? GRID_W - 2 : GRID_W;
+        int gh = (i & 1) ? GRID_H - 1 : GRID_H;
+
+        _textgrid_grid_resize(tg, gw, gh);
+        _bg_model_fill(model, gw, gh, i < 2);
+        _model_apply(tg, model, 0, 0, gw - 1, gh - 1);
+        _model_snapshot_check(ee, model, gw, gh);
+        _bg_model_fill(model, gw, gh, i >= 2);
+        _model_apply(tg, model, 0, 0, gw - 1, gh - 1);
+        _model_snapshot_check(ee, model, gw, gh);
+     }
+
+   END_TG_TEST();
+}
+EFL_END_TEST
+
 /* Glyph arrays are shared between the cells showing the same character.
  * Dropping what the font keeps in cache, as when memory runs low, must
  * leave the output as it was, for cells redrawn and for cells new. */
@@ -567,7 +1016,16 @@ void evas_test_textgrid(TCase *tc)
    tcase_add_test(tc, evas_textgrid_test_truecolor_resize_zeroed);
    tcase_add_test(tc, evas_textgrid_test_truecolor_disable_fallback);
    tcase_add_test(tc, evas_textgrid_test_truecolor_cellrow_set);
+   tcase_add_test(tc, evas_textgrid_test_bg_cells_exact);
+   tcase_add_test(tc, evas_textgrid_test_bg_partial_update);
+   tcase_add_test(tc, evas_textgrid_test_bg_transparent);
+   tcase_add_test(tc, evas_textgrid_test_bg_resize);
+   tcase_add_test(tc, evas_textgrid_test_bg_palette_change);
+   tcase_add_test(tc, evas_textgrid_test_bg_clipped);
+   tcase_add_test(tc, evas_textgrid_test_bg_engine_image_dropped);
+   tcase_add_test(tc, evas_textgrid_test_bg_render_op_mul);
    tcase_add_test(tc, evas_textgrid_test_text_cells_independent);
+   tcase_add_test(tc, evas_textgrid_test_bg_form_switch);
    tcase_add_test(tc, evas_textgrid_test_text_after_font_cache_flush);
 }
 

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

Reply via email to