Re: [AD] slow fonts

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


I've traced down the problem (at least on my machine) to the invocation of FT_Load_Glyph. According to the FreeType documentation this function is very slow, and removing its call from the addon sped up ex_ttf 3-4 times for me. The only thing we use FT_Load_Glyph during normal operation (i.e. after we cached the glyph) is to get the new advance values. I figured that there should be no problem in caching these advance values, so that FT_Load_Glyph call can be eliminated. In my testing I did not see any apparent difference between cached and non-cached versions of the code, while seeing significant speedup: ex_ttf went from 150 FPS to 550 FPS for me. The diff is attached.

-SiegeLord


      
Index: addons/ttf/ttf.c
===================================================================
--- addons/ttf/ttf.c	(revision 12308)
+++ addons/ttf/ttf.c	(working copy)
@@ -25,6 +25,7 @@
     int cache_pos_y;
     int cache_line_height;
     ALLEGRO_TTF_GLYPH_DATA *cache;
+    FT_Pos *advance_cache;
     int flags;
 } ALLEGRO_TTF_FONT_DATA;
 
@@ -96,10 +97,11 @@
     int ft_index = FT_Get_Char_Index(face, ch);
     unsigned char *row;
     int startpos = xpos;
+    int advance = 0;
 
     ALLEGRO_TTF_GLYPH_DATA *glyph = data->cache + ft_index;
     if (glyph->bitmap) {
-        FT_Load_Glyph(face, ft_index, FT_LOAD_DEFAULT);
+        advance = data->advance_cache[ft_index];
     }
     else {
         // FIXME: make this a config setting? FT_LOAD_FORCE_AUTOHINT
@@ -140,6 +142,8 @@
         al_unlock_bitmap(glyph->bitmap);
         glyph->x = face->glyph->bitmap_left;
         glyph->y = (face->size->metrics.ascender >> 6) - face->glyph->bitmap_top;
+        advance = face->glyph->advance.x >> 6;
+        data->advance_cache[ft_index] = advance;
 
         al_restore_state(&backup);
     }
@@ -157,7 +161,7 @@
     else
         al_draw_bitmap(glyph->bitmap, xpos + glyph->x, ypos + glyph->y , 0);
 
-    xpos += face->glyph->advance.x >> 6;
+    xpos += advance;
     return xpos - startpos;
 }
 
@@ -270,6 +274,7 @@
     }
     _al_vector_free(&data->cache_bitmaps);
     _AL_FREE(data->cache);
+    _AL_FREE(data->advance_cache);
     _AL_FREE(data);
     _AL_FREE(f);
 }
@@ -357,6 +362,11 @@
     bytes = (m + 1) * sizeof(ALLEGRO_TTF_GLYPH_DATA);
     data->cache = _AL_MALLOC(bytes);
     memset(data->cache, 0, bytes);
+
+    bytes = (m + 1) * sizeof(FT_Pos);
+    data->advance_cache = _AL_MALLOC(bytes);    
+    memset(data->advance_cache, 0, bytes);
+
     _al_vector_init(&data->cache_bitmaps, sizeof(ALLEGRO_BITMAP*));
     ALLEGRO_DEBUG("%s: Preparing cache for %d glyphs.\n", filename, m);
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/