Changeset 5b7951727f09e4a75f20b8b6f3155c9c8da72033

Show
Ignore:
Timestamp:
25/11/07 06:05:09 (1 year ago)
Author:
Bernie Purcell <bitmap@videolan.org>
git-committer:
Bernie Purcell <bitmap@videolan.org> 1195967109 +0000
git-parent:

[beb2d2fa9ed4c0de648c6016416f8712b84b2583]

git-author:
Bernie Purcell <bitmap@videolan.org> 1195967109 +0000
Message:

Try to protect more than one instance of FontBuilder? from running at a time -
attempt to fix issues with segfault reported on list

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/misc/freetype.c

    r0d38084 r5b79517  
    9292static char *FontConfig_Select( FcConfig *, const char *, 
    9393                                vlc_bool_t, vlc_bool_t, int * ); 
     94static int CheckIfFontBuildComplete( filter_t *p_filter ); 
    9495#endif 
    9596static line_desc_t *NewLine( int ); 
     
    276277static int Create( vlc_object_t *p_this ) 
    277278{ 
    278     filter_t *p_filter = (filter_t *)p_this; 
    279     filter_sys_t *p_sys; 
    280     char *psz_fontfile = NULL; 
    281     int i_error; 
    282     vlc_value_t val; 
     279    filter_t      *p_filter = (filter_t *)p_this; 
     280    filter_sys_t  *p_sys; 
     281    char          *psz_fontfile = NULL; 
     282    int            i_error; 
     283    vlc_value_t    val; 
     284    vlc_mutex_t   *lock; 
     285    vlc_object_t  *p_fontbuilder; 
    283286 
    284287    /* Allocate structure */ 
     
    364367    vlc_mutex_init( p_filter, &p_sys->fontconfig_lock ); 
    365368    p_sys->b_fontconfig_ok = VLC_FALSE; 
    366  
    367     p_sys->p_fontconfig = FcInitLoadConfig(); 
    368  
    369     if( p_sys->p_fontconfig ) 
    370     { 
    371         /* Normally this doesn't take very long, but an initial build of 
    372          * the fontconfig database or the addition of a lot of new fonts 
    373          * can cause it to take several minutes for a large number of fonts. 
    374          * Even a small number can take several seconds - much longer than 
    375          * we can afford to block, so we build the list in the background 
    376          * and if it succeeds we allow fontconfig to be used. 
     369    p_sys->p_fontconfig    = NULL; 
     370 
     371    /* Check for an existing Fontbuilder thread */ 
     372    lock = var_AcquireMutex( "fontbuilder" ); 
     373    p_fontbuilder = vlc_object_find_name( p_filter->p_libvlc, 
     374                                          "fontlist builder", 
     375                                          FIND_CHILD ); 
     376 
     377    if( ! p_fontbuilder ) 
     378    { 
     379        /* Create the FontBuilder thread as a child of a top-level 
     380         * object, so that it can survive the destruction of the 
     381         * freetype object - the fontlist only needs to be built once, 
     382         * and calling the fontbuild a second time while the first is 
     383         * still in progress can cause thread instabilities. 
    377384         */ 
    378         if( vlc_thread_create( p_filter, "fontlist builder", FontBuilder, 
    379                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) ) 
    380         { 
    381             /* Don't destroy the fontconfig object - we won't be able to do 
    382              * italics or bold or change the font face, but we will still 
    383              * be able to do underline and change the font size. 
    384              */ 
    385             msg_Warn( p_filter, "fontconfig database builder thread can't " 
    386                     "be launched. Font styling support will be limited." ); 
    387         }; 
     385 
     386        p_fontbuilder = vlc_object_create( p_filter->p_libvlc, 
     387                                           VLC_OBJECT_GENERIC ); 
     388        if( p_fontbuilder ) 
     389        { 
     390            p_fontbuilder->psz_object_name = "fontlist builder"; 
     391            vlc_object_attach( p_fontbuilder, p_filter->p_libvlc ); 
     392 
     393            var_Create( p_fontbuilder, "build-done", VLC_VAR_BOOL ); 
     394            var_SetBool( p_fontbuilder, "build-done", VLC_FALSE ); 
     395 
     396            if( vlc_thread_create( p_fontbuilder, 
     397                                   "fontlist builder", 
     398                                   FontBuilder, 
     399                                   VLC_THREAD_PRIORITY_LOW, 
     400                                   VLC_FALSE ) ) 
     401            { 
     402                /* Don't destroy the fontconfig object - we won't be able to do 
     403                 * italics or bold or change the font face, but we will still 
     404                 * be able to do underline and change the font size. 
     405                 */ 
     406                msg_Warn( p_filter, "fontconfig database builder thread can't " 
     407                        "be launched. Font styling support will be limited." ); 
     408            } 
     409        } 
     410        else 
     411        { 
     412            vlc_object_destroy( p_fontbuilder ); 
     413        } 
    388414    } 
    389415    else 
    390416    { 
    391         msg_Warn( p_filter, "Couldn't initialise Fontconfig. " 
    392                             "Font styling won't be available." ); 
    393     } 
     417        vlc_object_release( p_fontbuilder ); 
     418    } 
     419    vlc_mutex_unlock( lock ); 
     420 
    394421#endif 
    395422 
     
    407434    p_filter->pf_render_text = RenderText; 
    408435#ifdef HAVE_FONTCONFIG 
    409     if( p_sys->p_fontconfig ) 
    410         p_filter->pf_render_html = RenderHtml; 
    411     else 
     436    p_filter->pf_render_html = RenderHtml; 
     437#else 
     438    p_filter->pf_render_html = NULL; 
    412439#endif 
    413         p_filter->pf_render_html = NULL; 
    414440 
    415441    LoadFontsFromAttachments( p_filter ); 
     
    448474 
    449475#ifdef HAVE_FONTCONFIG 
    450     /* wait for the FontBuilder thread to terminate */ 
    451     vlc_thread_join( p_this ); 
    452476    vlc_mutex_destroy( &p_sys->fontconfig_lock ); 
    453477 
     
    470494static void FontBuilder( vlc_object_t *p_this ) 
    471495{ 
    472     filter_t *p_filter = (filter_t*)p_this; 
    473     filter_sys_t *p_sys = p_filter->p_sys; 
    474     time_t    t1, t2; 
    475  
    476     /* Find the session to announce */ 
    477     vlc_mutex_lock( &p_sys->fontconfig_lock ); 
    478  
    479     msg_Dbg( p_filter, "Building font database..." ); 
    480     time(&t1); 
    481     if(! FcConfigBuildFonts( p_sys->p_fontconfig )) 
    482     { 
    483         /* Don't destroy the fontconfig object - we won't be able to do 
    484          * italics or bold or change the font face, but we will still 
    485          * be able to do underline and change the font size. 
    486          */ 
    487         msg_Err( p_filter, "fontconfig database can't be built. " 
    488                                 "Font styling won't be available" ); 
    489     } 
    490     time(&t2); 
    491  
    492     msg_Dbg( p_filter, "Finished building font database." ); 
    493     if( t1 > 0 && t2 > 0 ) 
    494         msg_Dbg( p_filter, "Took %ld seconds", t2 - t1 ); 
    495  
    496     p_sys->b_fontconfig_ok = VLC_TRUE; 
    497  
    498     vlc_mutex_unlock( &p_sys->fontconfig_lock ); 
     496    FcConfig      *p_fontconfig = FcInitLoadConfig(); 
     497    vlc_mutex_t   *lock; 
     498 
     499    vlc_thread_ready( p_this ); 
     500 
     501    if( p_fontconfig ) 
     502    { 
     503        time_t    t1, t2; 
     504 
     505        msg_Dbg( p_this, "Building font database..." ); 
     506        time( &t1 ); 
     507        if(! FcConfigBuildFonts( p_fontconfig )) 
     508        { 
     509            /* Don't destroy the fontconfig object - we won't be able to do 
     510             * italics or bold or change the font face, but we will still 
     511             * be able to do underline and change the font size. 
     512             */ 
     513            msg_Err( p_this, "fontconfig database can't be built. " 
     514                                    "Font styling won't be available" ); 
     515        } 
     516        time( &t2 ); 
     517 
     518        msg_Dbg( p_this, "Finished building font database." ); 
     519        if( t1 > 0 && t2 > 0 ) 
     520            msg_Dbg( p_this, "Took %ld seconds", t2 - t1 ); 
     521 
     522        lock = var_AcquireMutex( "fontbuilder" ); 
     523        var_SetBool( p_this, "build-done", VLC_TRUE ); 
     524 
     525        FcConfigDestroy( p_fontconfig ); 
     526        vlc_mutex_unlock( lock ); 
     527    } 
    499528} 
    500529 
     
    21542183} 
    21552184 
     2185static int CheckIfFontBuildComplete( filter_t *p_filter ) 
     2186{ 
     2187    filter_sys_t   *p_sys = p_filter->p_sys; 
     2188    vlc_object_t   *p_fb = vlc_object_find_name( p_filter->p_libvlc, 
     2189                                                 "fontlist builder", 
     2190                                                 FIND_CHILD ); 
     2191    if( p_fb ) 
     2192    { 
     2193        vlc_mutex_t *lock = var_AcquireMutex( "fontbuilder" ); 
     2194        vlc_value_t  val; 
     2195 
     2196        if( VLC_SUCCESS == var_Get( p_fb, "build-done", &val )) 
     2197        { 
     2198            p_sys->b_fontconfig_ok = val.b_bool; 
     2199 
     2200            if( p_sys->b_fontconfig_ok ) 
     2201            { 
     2202                FcInit(); 
     2203                p_sys->p_fontconfig = FcConfigGetCurrent(); 
     2204            } 
     2205            else 
     2206                msg_Dbg( p_filter, "Font Build still not complete" ); 
     2207        } 
     2208        vlc_mutex_unlock( lock ); 
     2209        vlc_object_release( p_fb ); 
     2210 
     2211        return VLC_SUCCESS; 
     2212    } 
     2213    return VLC_EGENERIC; 
     2214} 
     2215 
    21562216static int ProcessLines( filter_t *p_filter, 
    21572217                         uint32_t *psz_text, 
     
    23702430            CheckForEmbeddedFont( p_sys, &p_face, p_style ); 
    23712431 
     2432            if( !p_sys->b_fontconfig_ok ) 
     2433            { 
     2434                if( VLC_EGENERIC == CheckIfFontBuildComplete( p_filter )) 
     2435                    msg_Err( p_filter, "Can't find FontBuilder thread!" ); 
     2436            } 
     2437 
    23722438            if( ! p_face && p_sys->b_fontconfig_ok ) 
    23732439            { 
    23742440                char *psz_fontfile; 
    2375  
    23762441                vlc_mutex_lock( &p_sys->fontconfig_lock ); 
    23772442 
     
    23812446                                                  p_style->b_italic, 
    23822447                                                  &i_idx ); 
    2383  
    23842448                vlc_mutex_unlock( &p_sys->fontconfig_lock ); 
    23852449