[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ft] Freetype memory allocation
From: |
R0b0t1 |
Subject: |
Re: [ft] Freetype memory allocation |
Date: |
Fri, 8 Sep 2017 19:09:12 -0500 |
On Fri, Sep 8, 2017 at 2:21 PM, Vincenzo NZ <address@hidden> wrote:
> Hi all,
> I have a little problem with freetype memory allocation.
> Basically I start from the tutorial example:
>
> FT_GlyphSlot slot = face->glyph; /* a small shortcut */
> int pen_x, pen_y, n;
>
>
> ... initialize library ...
> ... create face object ...
> ... set character size ...
>
> pen_x = 300;
> pen_y = 200;
>
> for ( n = 0; n < num_chars; n++ )
> {
> FT_UInt glyph_index;
>
>
> /* retrieve glyph index from character code */
> glyph_index = FT_Get_Char_Index( face, text[n] );
>
> /* load glyph image into the slot (erase previous one) */
> error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
> if ( error )
> continue; /* ignore errors */
>
> /* convert to an anti-aliased bitmap */
> error = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
> if ( error )
> continue;
>
> /* now, draw to our target surface */
> my_draw_bitmap( &slot->bitmap,
> pen_x + slot->bitmap_left,
> pen_y - slot->bitmap_top );
>
> /* increment pen position */
> pen_x += slot->advance.x >> 6;
> pen_y += slot->advance.y >> 6; /* not useful for now */
> }
>
> My main problem is that in my application *my_draw_bitmap* does not
> immediately draw the bitmap but instead adds the operation in a sort of
> queue that will be later flushed. The problem is that freetype at every new
> loop deallocates the memory allocated for the bitmap invalidating the
> address passed to *my_draw_bitmap*. I cannnot move the flush of the drawing
> operations at the end of the for loop for performances reason of my
> embedded application. Is there a clean way to avoid the automatic memory
> deallocation in freetype?
>
It looks like you will need to save the glyph bitmap yourself, or use
the FreeType cache. Otherwise you are expecting to get something for
nothing.
I don't mean to suggest the caching interface is trivial to
understand, but I recommend starting with the documentation if you
want to use it:
https://www.freetype.org/freetype2/docs/reference/ft2-cache_subsystem.html.
In practice people seem to do this by themselves (use a std::map to
associate utf-8 strings with glyph bitmaps or texture handles) and do
not use the caching mechanisms offered by FreeType. It seems like the
FreeType caching mechanism does not support GPU acceleration which is
probably one of the major reasons why people do not use it.
Cheers,
R0b0t1