LCOV - code coverage report
Current view: top level - vcl/generic/glyphs - glyphcache.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 141 200 70.5 %
Date: 2014-04-11 Functions: 22 26 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stdio.h>
      21             : #include <stdlib.h>
      22             : #include <math.h>
      23             : #include <gcach_ftyp.hxx>
      24             : 
      25             : #include <vcl/svapp.hxx>
      26             : #include <vcl/bitmap.hxx>
      27             : #include <outfont.hxx>
      28             : 
      29             : #include <config_graphite.h>
      30             : #if ENABLE_GRAPHITE
      31             : #include <graphite_features.hxx>
      32             : #endif
      33             : 
      34             : #include <rtl/ustring.hxx>
      35             : #include <osl/file.hxx>
      36             : #include <tools/debug.hxx>
      37             : 
      38             : static GlyphCache* pInstance = NULL;
      39             : 
      40         136 : GlyphCache::GlyphCache( GlyphCachePeer& rPeer )
      41             : :   mrPeer( rPeer ),
      42             :     mnMaxSize( 1500000 ),
      43             :     mnBytesUsed(sizeof(GlyphCache)),
      44             :     mnLruIndex(0),
      45             :     mnGlyphCount(0),
      46             :     mpCurrentGCFont(NULL),
      47         136 :     mpFtManager(NULL)
      48             : {
      49         136 :     pInstance = this;
      50         136 :     mpFtManager = new FreetypeManager;
      51         136 : }
      52             : 
      53         272 : GlyphCache::~GlyphCache()
      54             : {
      55         136 :     InvalidateAllGlyphs();
      56         136 :     delete mpFtManager;
      57         136 : }
      58             : 
      59         136 : void GlyphCache::InvalidateAllGlyphs()
      60             : {
      61        3838 :     for( FontList::iterator it = maFontList.begin(), end = maFontList.end(); it != end; ++it )
      62             :     {
      63        3702 :         ServerFont* pServerFont = it->second;
      64        3702 :         mrPeer.RemovingFont(*pServerFont);
      65        3702 :         delete pServerFont;
      66             :     }
      67             : 
      68         136 :     maFontList.clear();
      69         136 :     mpCurrentGCFont = NULL;
      70         136 : }
      71             : 
      72             : inline
      73      195751 : size_t GlyphCache::IFSD_Hash::operator()( const FontSelectPattern& rFontSelData ) const
      74             : {
      75             :     // TODO: is it worth to improve this hash function?
      76      195751 :     sal_IntPtr nFontId = reinterpret_cast<sal_IntPtr>( rFontSelData.mpFontData );
      77             : #if ENABLE_GRAPHITE
      78      195751 :     if (rFontSelData.maTargetName.indexOf(grutils::GrFeatureParser::FEAT_PREFIX)
      79             :         != -1)
      80             :     {
      81           0 :         OString aFeatName = OUStringToOString( rFontSelData.maTargetName, RTL_TEXTENCODING_UTF8 );
      82           0 :         nFontId ^= aFeatName.hashCode();
      83             :     }
      84             : #endif
      85      195751 :     size_t nHash = nFontId << 8;
      86      195751 :     nHash   += rFontSelData.mnHeight;
      87      195751 :     nHash   += rFontSelData.mnOrientation;
      88      195751 :     nHash   += size_t(rFontSelData.mbVertical);
      89      195751 :     nHash   += rFontSelData.GetSlant();
      90      195751 :     nHash   += rFontSelData.GetWeight();
      91             : #if ENABLE_GRAPHITE
      92      195751 :     nHash   += rFontSelData.meLanguage;
      93             : #endif
      94      195751 :     return nHash;
      95             : }
      96             : 
      97      200569 : bool GlyphCache::IFSD_Equal::operator()( const FontSelectPattern& rA, const FontSelectPattern& rB) const
      98             : {
      99             :     // check font ids
     100      200569 :     sal_IntPtr nFontIdA = reinterpret_cast<sal_IntPtr>( rA.mpFontData );
     101      200569 :     sal_IntPtr nFontIdB = reinterpret_cast<sal_IntPtr>( rB.mpFontData );
     102      200569 :     if( nFontIdA != nFontIdB )
     103          17 :         return false;
     104             : 
     105             :     // compare with the requested metrics
     106      200552 :     if( (rA.mnHeight         != rB.mnHeight)
     107      200319 :     ||  (rA.mnOrientation    != rB.mnOrientation)
     108      200319 :     ||  (rA.mbVertical       != rB.mbVertical)
     109      200319 :     ||  (rA.mbNonAntialiased != rB.mbNonAntialiased) )
     110        6693 :         return false;
     111             : 
     112      387718 :     if( (rA.GetSlant() != rB.GetSlant())
     113      193859 :     ||  (rA.GetWeight() != rB.GetWeight()) )
     114           0 :         return false;
     115             : 
     116             :     // NOTE: ignoring meFamily deliberately
     117             : 
     118             :     // compare with the requested width, allow default width
     119      193859 :     int nAWidth = rA.mnWidth != 0 ? rA.mnWidth : rA.mnHeight;
     120      193859 :     int nBWidth = rB.mnWidth != 0 ? rB.mnWidth : rB.mnHeight;
     121      193859 :     if( nAWidth != nBWidth )
     122        5512 :         return false;
     123             : 
     124             : #if ENABLE_GRAPHITE
     125      188347 :    if (rA.meLanguage != rB.meLanguage)
     126           0 :         return false;
     127             :    // check for features
     128      376694 :    if ((rA.maTargetName.indexOf(grutils::GrFeatureParser::FEAT_PREFIX)
     129      188347 :         != -1 ||
     130      188347 :         rB.maTargetName.indexOf(grutils::GrFeatureParser::FEAT_PREFIX)
     131      188347 :         != -1) && rA.maTargetName != rB.maTargetName)
     132           0 :         return false;
     133             : #endif
     134             : 
     135      188347 :     if (rA.mbEmbolden != rB.mbEmbolden)
     136           0 :         return false;
     137             : 
     138      188347 :     if (rA.maItalicMatrix != rB.maItalicMatrix)
     139           0 :         return false;
     140             : 
     141      188347 :     return true;
     142             : }
     143             : 
     144    16453271 : GlyphCache& GlyphCache::GetInstance()
     145             : {
     146    16453271 :     return *pInstance;
     147             : }
     148             : 
     149       28980 : void GlyphCache::AddFontFile( const OString& rNormalizedName, int nFaceNum,
     150             :     sal_IntPtr nFontId, const ImplDevFontAttributes& rDFA)
     151             : {
     152       28980 :     if( mpFtManager )
     153       28980 :         mpFtManager->AddFontFile( rNormalizedName, nFaceNum, nFontId, rDFA);
     154       28980 : }
     155             : 
     156         138 : void GlyphCache::AnnounceFonts( PhysicalFontCollection* pFontCollection ) const
     157             : {
     158         138 :     if( mpFtManager )
     159         138 :         mpFtManager->AnnounceFonts( pFontCollection );
     160         138 : }
     161             : 
     162           0 : void GlyphCache::ClearFontCache()
     163             : {
     164           0 :     InvalidateAllGlyphs();
     165           0 :     if (mpFtManager)
     166           0 :         mpFtManager->ClearFontList();
     167           0 : }
     168             : 
     169      192049 : ServerFont* GlyphCache::CacheFont( const FontSelectPattern& rFontSelData )
     170             : {
     171             :     // a serverfont request has pFontData
     172      192049 :     if( rFontSelData.mpFontData == NULL )
     173           0 :         return NULL;
     174             :     // a serverfont request has a fontid > 0
     175      192049 :     sal_IntPtr nFontId = rFontSelData.mpFontData->GetFontId();
     176      192049 :     if( nFontId <= 0 )
     177           0 :         return NULL;
     178             : 
     179             :     // the FontList's key mpFontData member is reinterpreted as font id
     180      192049 :     FontSelectPattern aFontSelData = rFontSelData;
     181      192049 :     aFontSelData.mpFontData = reinterpret_cast<PhysicalFontFace*>( nFontId );
     182      192049 :     FontList::iterator it = maFontList.find( aFontSelData );
     183      192049 :     if( it != maFontList.end() )
     184             :     {
     185      188347 :         ServerFont* pFound = it->second;
     186      188347 :         if( pFound )
     187      188347 :             pFound->AddRef();
     188      188347 :         return pFound;
     189             :     }
     190             : 
     191             :     // font not cached yet => create new font item
     192        3702 :     ServerFont* pNew = NULL;
     193        3702 :     if( mpFtManager )
     194        3702 :         pNew = mpFtManager->CreateFont( aFontSelData );
     195             : 
     196        3702 :     if( pNew )
     197             :     {
     198        3702 :         maFontList[ aFontSelData ] = pNew;
     199        3702 :         mnBytesUsed += pNew->GetByteCount();
     200             : 
     201             :         // enable garbage collection for new font
     202        3702 :         if( !mpCurrentGCFont )
     203             :         {
     204         135 :             mpCurrentGCFont = pNew;
     205         135 :             pNew->mpNextGCFont = pNew;
     206         135 :             pNew->mpPrevGCFont = pNew;
     207             :         }
     208             :         else
     209             :         {
     210        3567 :             pNew->mpNextGCFont = mpCurrentGCFont;
     211        3567 :             pNew->mpPrevGCFont = mpCurrentGCFont->mpPrevGCFont;
     212        3567 :             pNew->mpPrevGCFont->mpNextGCFont = pNew;
     213        3567 :             mpCurrentGCFont->mpPrevGCFont = pNew;
     214             :         }
     215             :     }
     216             : 
     217        3702 :     return pNew;
     218             : }
     219             : 
     220      191775 : void GlyphCache::UncacheFont( ServerFont& rServerFont )
     221             : {
     222             :     // the interface for rServerFont must be const because a
     223             :     // user who wants to release it only got const ServerFonts.
     224             :     // The caching algorithm needs a non-const object
     225      191775 :     ServerFont* pFont = const_cast<ServerFont*>( &rServerFont );
     226      383550 :     if( (pFont->Release() <= 0)
     227      191775 :     &&  (mnMaxSize <= (mnBytesUsed + mrPeer.GetByteCount())) )
     228             :     {
     229           0 :         mpCurrentGCFont = pFont;
     230           0 :         GarbageCollect();
     231             :     }
     232      191775 : }
     233             : 
     234           0 : void GlyphCache::GarbageCollect()
     235             : {
     236             :     // when current GC font has been destroyed get another one
     237           0 :     if( !mpCurrentGCFont )
     238             :     {
     239           0 :         FontList::iterator it = maFontList.begin();
     240           0 :         if( it != maFontList.end() )
     241           0 :             mpCurrentGCFont = it->second;
     242             :     }
     243             : 
     244             :     // unless there is no other font to collect
     245           0 :     if( !mpCurrentGCFont )
     246           0 :         return;
     247             : 
     248             :     // prepare advance to next font for garbage collection
     249           0 :     ServerFont* const pServerFont = mpCurrentGCFont;
     250           0 :     mpCurrentGCFont = pServerFont->mpNextGCFont;
     251             : 
     252           0 :     if( (pServerFont == mpCurrentGCFont)    // no other fonts
     253           0 :     ||  (pServerFont->GetRefCount() > 0) )  // font still used
     254             :     {
     255             :         // try to garbage collect at least a few bytes
     256           0 :         pServerFont->GarbageCollect( mnLruIndex - mnGlyphCount/2 );
     257             :     }
     258             :     else // current GC font is unreferenced
     259             :     {
     260             :         DBG_ASSERT( (pServerFont->GetRefCount() == 0),
     261             :             "GlyphCache::GC detected RefCount underflow" );
     262             : 
     263             :         // free all pServerFont related data
     264           0 :         pServerFont->GarbageCollect( mnLruIndex+0x10000000 );
     265           0 :         if( pServerFont == mpCurrentGCFont )
     266           0 :             mpCurrentGCFont = NULL;
     267           0 :         const FontSelectPattern& rIFSD = pServerFont->GetFontSelData();
     268           0 :         maFontList.erase( rIFSD );
     269           0 :         mrPeer.RemovingFont( *pServerFont );
     270           0 :         mnBytesUsed -= pServerFont->GetByteCount();
     271             : 
     272             :         // remove font from list of garbage collected fonts
     273           0 :         if( pServerFont->mpPrevGCFont )
     274           0 :             pServerFont->mpPrevGCFont->mpNextGCFont = pServerFont->mpNextGCFont;
     275           0 :         if( pServerFont->mpNextGCFont )
     276           0 :             pServerFont->mpNextGCFont->mpPrevGCFont = pServerFont->mpPrevGCFont;
     277           0 :         if( pServerFont == mpCurrentGCFont )
     278           0 :             mpCurrentGCFont = NULL;
     279             : 
     280           0 :         delete pServerFont;
     281             :     }
     282             : }
     283             : 
     284    16452528 : inline void GlyphCache::UsingGlyph( ServerFont&, GlyphData& rGlyphData )
     285             : {
     286    16452528 :     rGlyphData.SetLruValue( mnLruIndex++ );
     287    16452528 : }
     288             : 
     289       42298 : inline void GlyphCache::AddedGlyph( ServerFont& rServerFont, GlyphData& rGlyphData )
     290             : {
     291       42298 :     ++mnGlyphCount;
     292       42298 :     mnBytesUsed += sizeof( rGlyphData );
     293       42298 :     UsingGlyph( rServerFont, rGlyphData );
     294       42298 :     GrowNotify();
     295       42298 : }
     296             : 
     297       42298 : void GlyphCache::GrowNotify()
     298             : {
     299       42298 :     if( (mnBytesUsed + mrPeer.GetByteCount()) > mnMaxSize )
     300           0 :         GarbageCollect();
     301       42298 : }
     302             : 
     303           0 : inline void GlyphCache::RemovingGlyph( GlyphData& rGD )
     304             : {
     305           0 :     mrPeer.RemovingGlyph( rGD );
     306           0 :     mnBytesUsed -= sizeof( GlyphData );
     307           0 :     --mnGlyphCount;
     308           0 : }
     309             : 
     310        3702 : void ServerFont::ReleaseFromGarbageCollect()
     311             : {
     312             :     // remove from GC list
     313        3702 :     ServerFont* pPrev = mpPrevGCFont;
     314        3702 :     ServerFont* pNext = mpNextGCFont;
     315        3702 :     if( pPrev ) pPrev->mpNextGCFont = pNext;
     316        3702 :     if( pNext ) pNext->mpPrevGCFont = pPrev;
     317        3702 :     mpPrevGCFont = NULL;
     318        3702 :     mpNextGCFont = NULL;
     319        3702 : }
     320             : 
     321      194584 : long ServerFont::Release() const
     322             : {
     323             :     DBG_ASSERT( mnRefCount > 0, "ServerFont: RefCount underflow" );
     324      194584 :     return --mnRefCount;
     325             : }
     326             : 
     327    16452528 : GlyphData& ServerFont::GetGlyphData( sal_GlyphId aGlyphId )
     328             : {
     329             :     // usually the GlyphData is cached
     330    16452528 :     GlyphList::iterator it = maGlyphList.find( aGlyphId );
     331    16452528 :     if( it != maGlyphList.end() ) {
     332    16410230 :         GlyphData& rGlyphData = it->second;
     333    16410230 :         GlyphCache::GetInstance().UsingGlyph( *this, rGlyphData );
     334    16410230 :         return rGlyphData;
     335             :     }
     336             : 
     337             :     // sometimes not => we need to create and initialize it ourselves
     338       42298 :     GlyphData& rGlyphData = maGlyphList[ aGlyphId ];
     339       42298 :     mnBytesUsed += sizeof( GlyphData );
     340       42298 :     InitGlyphData( aGlyphId, rGlyphData );
     341       42298 :     GlyphCache::GetInstance().AddedGlyph( *this, rGlyphData );
     342       42298 :     return rGlyphData;
     343             : }
     344             : 
     345           0 : void ServerFont::GarbageCollect( long nMinLruIndex )
     346             : {
     347           0 :     GlyphList::iterator it = maGlyphList.begin();
     348           0 :     while( it != maGlyphList.end() )
     349             :     {
     350           0 :         GlyphData& rGD = it->second;
     351           0 :         if( (nMinLruIndex - rGD.GetLruValue()) > 0 )
     352             :         {
     353             :             OSL_ASSERT( mnBytesUsed >= sizeof(GlyphData) );
     354           0 :             mnBytesUsed -= sizeof( GlyphData );
     355           0 :             GlyphCache::GetInstance().RemovingGlyph( rGD );
     356           0 :             it = maGlyphList.erase( it );
     357             :         }
     358             :         else
     359           0 :             ++it;
     360             :     }
     361           0 : }
     362             : 
     363        8826 : ImplServerFontEntry::ImplServerFontEntry( FontSelectPattern& rFSD )
     364             : :   ImplFontEntry( rFSD )
     365             : ,   mpServerFont( NULL )
     366        8826 : ,   mbGotFontOptions( false )
     367        8826 : {}
     368             : 
     369        3702 : void ImplServerFontEntry::SetServerFont(ServerFont* p)
     370             : {
     371        3702 :     if (p == mpServerFont)
     372        3702 :         return;
     373        3702 :     if (mpServerFont)
     374           0 :         mpServerFont->Release();
     375        3702 :     mpServerFont = p;
     376        3702 :     if (mpServerFont)
     377        3702 :         mpServerFont->AddRef();
     378             : }
     379             : 
     380       22746 : ImplServerFontEntry::~ImplServerFontEntry()
     381             : {
     382             :     // TODO: remove the ServerFont here instead of in the GlyphCache
     383        7582 :     if (mpServerFont)
     384        2809 :         mpServerFont->Release();
     385       15680 : }
     386             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10