freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] master 13200abe: [graph] Rework #ifdef GBLENDER_STORE_


From: Werner Lemberg
Subject: [freetype2-demos] master 13200abe: [graph] Rework #ifdef GBLENDER_STORE_BYTES.
Date: Sun, 28 Aug 2022 22:36:45 -0400 (EDT)

branch: master
commit 13200abeb1bbc5adcfe752d74c7a366257e43b85
Author: Alexei Podtelezhnikov <apodtele@gmail.com>
Commit: Alexei Podtelezhnikov <apodtele@gmail.com>

    [graph] Rework #ifdef GBLENDER_STORE_BYTES.
    
    This also settles on tripling the number of channel keys, which is
    a safe bet in all circumstances.  Quadrupling might only work on
    64-bit architectures or with added key structure padding, without
    GBLENDER_STORE_BYTES defined.
    
    * graph/gblender.h (GBlenderCell): Change definition.
    * graph/gblblit.c (GDST_STOREB): Updated accordingly.
    * graph/gblender.c (gblender_clear, gblender_reset_key,
      gblender_lookup, gblender_lookup_channel): Ditto.
---
 graph/gblblit.c  | 54 +++++++++++++++++++++++++++---------------------------
 graph/gblender.c | 30 ++++++++++++------------------
 graph/gblender.h |  8 ++++----
 3 files changed, 43 insertions(+), 49 deletions(-)

diff --git a/graph/gblblit.c b/graph/gblblit.c
index d847cf73..bf02918b 100644
--- a/graph/gblblit.c
+++ b/graph/gblblit.c
@@ -106,11 +106,11 @@
 #define  GDST_PIX(p,d)            unsigned int  p = *(GBlenderPixel*)(d) & 
0xFFFFFF
 #define  GDST_COPY(d)             *(GBlenderPixel*)(d) = color.value
 #define  GDST_STOREP(d,cells,a)   *(GBlenderPixel*)(d) = (cells)[(a)]
-#define  GDST_STOREB(d,cells,a)              \
-  {                                          \
-    GBlenderCell*  _g = (cells) + (a)*3;     \
-                                             \
-    GDST_STOREC(d,_g[0],_g[1],_g[2]);        \
+#define  GDST_STOREB(d,cells,a)                 \
+  {                                             \
+    GBlenderCell*  _g = (cells) + (a);          \
+                                                \
+    GDST_STOREC(d,(*_g)[0],(*_g)[1],(*_g)[2]);  \
   }
 #define  GDST_STOREC(d,r,g,b)     *(GBlenderPixel*)(d) = GRGB_PACK(r,g,b)
 
@@ -128,13 +128,13 @@
 #define  GDST_COPY(d)              
GDST_STORE3(d,color.chroma[0],color.chroma[1],color.chroma[2])
 #define  GDST_STOREC(d,r,g,b)      GDST_STORE3(d,r,g,b)
 
-#define  GDST_STOREB(d,cells,a)                \
-    {                                          \
-      GBlenderCell*  _g = (cells) + (a)*3;     \
-                                               \
-      (d)[0] = _g[0];                          \
-      (d)[1] = _g[1];                          \
-      (d)[2] = _g[2];                          \
+#define  GDST_STOREB(d,cells,a)           \
+    {                                     \
+      GBlenderCell*  _g = (cells) + (a);  \
+                                          \
+      (d)[0] = (*_g)[0];                  \
+      (d)[1] = (*_g)[1];                  \
+      (d)[2] = (*_g)[2];                  \
     }
 
 #define  GDST_STOREP(d,cells,a)                 \
@@ -158,11 +158,11 @@
 #define  GDST_PIX(p,d)           unsigned int  p = GRGB565_TO_RGB24(*(unsigned 
short*)(d))
 #define  GDST_COPY(d)            *(unsigned short*)(d) = (unsigned 
short)color.value
 
-#define  GDST_STOREB(d,cells,a)                                   \
-    {                                                             \
-      GBlenderCell*  _g = (cells) + (a)*3;                        \
-                                                                  \
-      *(unsigned short*)(d) = GRGB_TO_RGB565(_g[0],_g[1],_g[2]);  \
+#define  GDST_STOREB(d,cells,a)                                            \
+    {                                                                      \
+      GBlenderCell*  _g = (cells) + (a);                                   \
+                                                                           \
+      *(unsigned short*)(d) = GRGB_TO_RGB565((*_g)[0],(*_g)[1],(*_g)[2]);  \
     }
 
 #define  GDST_STOREP(d,cells,a)                         \
@@ -187,11 +187,11 @@
 #define  GDST_PIX(p,d)           unsigned int  p = GRGB555_TO_RGB24(*(unsigned 
short*)(d))
 #define  GDST_COPY(d)            *(unsigned short*)(d) = (unsigned 
short)color.value
 
-#define  GDST_STOREB(d,cells,a)                                   \
-    {                                                             \
-      GBlenderCell*  _g = (cells) + (a)*3;                        \
-                                                                  \
-      *(unsigned short*)(d) = GRGB_TO_RGB555(_g[0],_g[1],_g[2]);  \
+#define  GDST_STOREB(d,cells,a)                                            \
+    {                                                                      \
+      GBlenderCell*  _g = (cells) + (a);                                   \
+                                                                           \
+      *(unsigned short*)(d) = GRGB_TO_RGB555((*_g)[0],(*_g)[1],(*_g)[2]);  \
     }
 
 #define  GDST_STOREP(d,cells,a)                         \
@@ -216,11 +216,11 @@
 #define  GDST_PIX(p,d)           unsigned int  p = GGRAY8_TO_RGB24(*(unsigned 
char*)(d))
 #define  GDST_COPY(d)            *(d) = (unsigned char)color.value
 
-#define  GDST_STOREB(d,cells,a)                 \
-    {                                           \
-      GBlenderCell*  _g = (cells) + (a)*3;      \
-                                                \
-      *(d) = GRGB_TO_GRAY8(_g[0],_g[1],_g[2]);  \
+#define  GDST_STOREB(d,cells,a)                          \
+    {                                                    \
+      GBlenderCell*  _g = (cells) + (a);                 \
+                                                         \
+      *(d) = GRGB_TO_GRAY8((*_g)[0],(*_g)[1],(*_g)[2]);  \
     }
 
 #define  GDST_STOREP(d,cells,a)           \
diff --git a/graph/gblender.c b/graph/gblender.c
index 6931bd49..8bb36207 100644
--- a/graph/gblender.c
+++ b/graph/gblender.c
@@ -126,9 +126,7 @@ gblender_clear( GBlender  blender )
   {
     GBlenderChanKey  chan_keys = (GBlenderChanKey) blender->keys;
 
-    for ( nn = 0;
-          nn < GBLENDER_KEY_COUNT*GBLENDER_CELL_SIZE*sizeof(GBlenderCell);
-          nn++ )
+    for ( nn = 0; nn < GBLENDER_KEY_COUNT * 3; nn++ )
       chan_keys[nn].index = -1;
 
     blender->cache_r_back  = 0;
@@ -215,14 +213,13 @@ gblender_reset_key( GBlender     blender,
   b2 = ( fore )       & 255;
 
 #ifdef GBLENDER_STORE_BYTES
-  gr[0] = (unsigned char)r1;
-  gr[1] = (unsigned char)g1;
-  gr[2] = (unsigned char)b1;
-  gr   += 3;
+  (*gr)[0] = (unsigned char)r1;
+  (*gr)[1] = (unsigned char)g1;
+  (*gr)[2] = (unsigned char)b1;
 #else
   gr[0] = back;
-  gr   += 1;
 #endif
+  gr++;
 
   r1 = gamma_ramp[r1] << 10;
   g1 = gamma_ramp[g1] << 10;
@@ -250,14 +247,13 @@ gblender_reset_key( GBlender     blender,
     b = gamma_ramp_inv[b1 >> 10];
 
 #ifdef GBLENDER_STORE_BYTES
-    gr[0] = r;
-    gr[1] = g;
-    gr[2] = b;
-    gr   += 3;
+    (*gr)[0] = r;
+    (*gr)[1] = g;
+    (*gr)[2] = b;
 #else
     gr[0] = ( r << 16 ) | ( g << 8 ) | b;
-    gr   += 1;
 #endif
+    gr++;
   }
 }
 
@@ -294,8 +290,7 @@ gblender_lookup( GBlender       blender,
 NewNode:
   key->background = background;
   key->foreground = foreground;
-  key->cells      = blender->cells +
-                    idx*(GBLENDER_SHADE_COUNT*GBLENDER_CELL_SIZE);
+  key->cells      = blender->cells + idx * GBLENDER_SHADE_COUNT;
 
   gblender_reset_key( blender, key );
 
@@ -354,8 +349,7 @@ gblender_lookup_channel( GBlender      blender,
   blender->stat_lookups++;
 #endif
 
-  idx = ( background ^ foreground * 59 ) %
-        ( GBLENDER_KEY_COUNT*GBLENDER_CELL_SIZE*sizeof(GBlenderCell) - 1);
+  idx = ( background ^ foreground * 59 ) % ( GBLENDER_KEY_COUNT * 3 - 1 );
 
   key = (GBlenderChanKey)blender->keys + idx;
 
@@ -390,7 +384,7 @@ Exit:
 GBLENDER_APIDEF( void )
 gblender_dump_stats( GBlender  blender )
 {
-  printf( "GBlender cache statistics:\n" );
+  printf( "GBlender cache (%zu bytes) statistics:\n", sizeof blender->cells );
   printf( "  Hit rate:    %.2f%% ( %ld out of %ld )\n",
           100.0f * blender->stat_hits /
                    ( blender->stat_hits + blender->stat_lookups ),
diff --git a/graph/gblender.h b/graph/gblender.h
index f5d27b69..85429618 100644
--- a/graph/gblender.h
+++ b/graph/gblender.h
@@ -38,11 +38,9 @@
   typedef unsigned int    GBlenderPixel;  /* needs 32-bits here !! */
 
 #ifdef GBLENDER_STORE_BYTES
-  typedef unsigned char   GBlenderCell;
-# define  GBLENDER_CELL_SIZE    3
+  typedef unsigned char   GBlenderCell[3];
 #else
   typedef GBlenderPixel   GBlenderCell;
-# define GBLENDER_CELL_SIZE     1
 #endif
 
 
@@ -70,10 +68,12 @@
   } GBlenderChanKeyRec, *GBlenderChanKey;
 
 
+  /* sizeof GBlenderKeyRec is at least 3x sizeof GBlenderChanKeyRec */
+  /* Therefore, we can safely use 3x as many channel keys           */
   typedef struct GBlenderRec_
   {
     GBlenderKeyRec        keys [ GBLENDER_KEY_COUNT ];
-    GBlenderCell          cells[ 
GBLENDER_KEY_COUNT*GBLENDER_SHADE_COUNT*GBLENDER_CELL_SIZE ];
+    GBlenderCell          cells[ GBLENDER_KEY_COUNT*GBLENDER_SHADE_COUNT ];
 
    /* a small cache for normal modes
     */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]