Qt 4.8
Classes | Public Types | Public Functions | Static Public Functions | Public Variables | Private Functions | Properties | Static Private Attributes | List of all members
QFontCache Class Reference

#include <qfont_p.h>

Inheritance diagram for QFontCache:
QObject

Classes

struct  Engine
 
struct  Key
 

Public Types

typedef QMap< Key, EngineEngineCache
 
typedef QMap< Key, QFontEngineData * > EngineDataCache
 

Public Functions

void cleanupPrinterFonts ()
 
void clear ()
 
QFontEnginefindEngine (const Key &key)
 
QFontEngineDatafindEngineData (const Key &key) const
 
void insertEngine (const Key &key, QFontEngine *engine)
 
void insertEngineData (const Key &key, QFontEngineData *engineData)
 
 QFontCache ()
 
void removeEngineForFont (const QByteArray &fontName)
 
 ~QFontCache ()
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Static Public Functions

static void cleanup ()
 
static QFontCacheinstance ()
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Public Variables

EngineCache engineCache
 
EngineDataCache engineDataCache
 

Private Functions

void decreaseCost (uint cost)
 
void increaseCost (uint cost)
 
void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Properties

uint current_timestamp
 
bool fast
 
uint max_cost
 
int timer_id
 
uint total_cost
 

Static Private Attributes

static const uint min_cost = 4*1024
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 213 of file qfont_p.h.

Typedefs

◆ EngineCache

Definition at line 266 of file qfont_p.h.

◆ EngineDataCache

Definition at line 250 of file qfont_p.h.

Constructors and Destructors

◆ QFontCache()

QFontCache::QFontCache ( )

Definition at line 2940 of file qfont.cpp.

2941  : QObject(), total_cost(0), max_cost(min_cost),
2942  current_timestamp(0), fast(false), timer_id(-1)
2943 {
2944 }
static const uint min_cost
Definition: qfont_p.h:281
uint total_cost
Definition: qfont_p.h:282
uint current_timestamp
Definition: qfont_p.h:283
uint max_cost
Definition: qfont_p.h:282
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
int timer_id
Definition: qfont_p.h:285
bool fast
Definition: qfont_p.h:284

◆ ~QFontCache()

QFontCache::~QFontCache ( )

Definition at line 2946 of file qfont.cpp.

2947 {
2948  clear();
2949  {
2952  while (it != end) {
2953  if (it.value()->ref == 0)
2954  delete it.value();
2955  else
2956  FC_DEBUG("QFontCache::~QFontCache: engineData %p still has refcount %d",
2957  it.value(), int(it.value()->ref));
2958  ++it;
2959  }
2960  }
2962  end = engineCache.constEnd();
2963  while (it != end) {
2964  if (--it.value().data->cache_count == 0) {
2965  if (it.value().data->ref == 0) {
2966  FC_DEBUG("QFontCache::~QFontCache: deleting engine %p key=(%d / %g %g %d %d %d)",
2967  it.value().data, it.key().script, it.key().def.pointSize,
2968  it.key().def.pixelSize, it.key().def.weight, it.key().def.style,
2969  it.key().def.fixedPitch);
2970 
2971  delete it.value().data;
2972  } else {
2973  FC_DEBUG("QFontCache::~QFontCache: engine = %p still has refcount %d",
2974  it.value().data, int(it.value().data->ref));
2975  }
2976  }
2977  ++it;
2978  }
2979 }
#define it(className, varName)
EngineCache engineCache
Definition: qfont_p.h:267
#define FC_DEBUG
Definition: qfont.cpp:89
const_iterator ConstIterator
Qt-style synonym for QMap::const_iterator.
Definition: qmap.h:389
EngineDataCache engineDataCache
Definition: qfont_p.h:251
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
const T & value() const
Returns the current item&#39;s value.
Definition: qmap.h:325
static const KeyPair *const end
void clear()
Definition: qfont.cpp:2981

Functions

◆ cleanup()

void QFontCache::cleanup ( )
static

Definition at line 2927 of file qfont.cpp.

Referenced by QFont::cleanup(), pointSize(), and qt_x11_set_fallback_font_family().

2928 {
2929  QThreadStorage<QFontCache *> *cache = 0;
2930  QT_TRY {
2931  cache = theFontCache();
2932  } QT_CATCH (const std::bad_alloc &) {
2933  // no cache - just ignore
2934  }
2935  if (cache && cache->hasLocalData())
2936  cache->setLocalData(0);
2937 }
#define QT_CATCH(A)
Definition: qglobal.h:1537
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
void setLocalData(T t)
Sets the local data for the calling thread to data.
The QThreadStorage class provides per-thread data storage.
#define QT_TRY
Definition: qglobal.h:1536

◆ cleanupPrinterFonts()

void QFontCache::cleanupPrinterFonts ( )

Definition at line 3123 of file qfont.cpp.

3124 {
3125  FC_DEBUG("QFontCache::cleanupPrinterFonts");
3126 
3127  {
3128  FC_DEBUG(" CLEAN engine data:");
3129 
3130  // clean out all unused engine data
3132  end = engineDataCache.end();
3133  while (it != end) {
3134  if (it.key().screen == 0) {
3135  ++it;
3136  continue;
3137  }
3138 
3139  if(it.value()->ref != 0) {
3140  for(int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
3141  if(it.value()->engines[i]) {
3142  it.value()->engines[i]->ref.deref();
3143  it.value()->engines[i] = 0;
3144  }
3145  }
3146  ++it;
3147  } else {
3148 
3149  EngineDataCache::Iterator rem = it++;
3150 
3151  decreaseCost(sizeof(QFontEngineData));
3152 
3153  FC_DEBUG(" %p", rem.value());
3154 
3155  delete rem.value();
3156  engineDataCache.erase(rem);
3157  }
3158  }
3159  }
3160 
3162  end = engineCache.end();
3163  while(it != end) {
3164  if (it.value().data->ref != 0 || it.key().screen == 0) {
3165  ++it;
3166  continue;
3167  }
3168 
3169  FC_DEBUG(" %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'",
3170  it.value().data, it.value().timestamp, it.value().hits,
3171  int(it.value().data->ref), it.value().data->cache_count,
3172  it.value().data->name());
3173 
3174  if (--it.value().data->cache_count == 0) {
3175  FC_DEBUG(" DELETE: last occurrence in cache");
3176 
3177  decreaseCost(it.value().data->cache_cost);
3178  delete it.value().data;
3179  }
3180 
3181  engineCache.erase(it++);
3182  }
3183 }
#define it(className, varName)
EngineCache engineCache
Definition: qfont_p.h:267
void decreaseCost(uint cost)
Definition: qfont.cpp:3111
#define FC_DEBUG
Definition: qfont.cpp:89
iterator Iterator
Qt-style synonym for QMap::iterator.
Definition: qmap.h:388
EngineDataCache engineDataCache
Definition: qfont_p.h:251
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
iterator erase(iterator it)
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to...
Definition: qmap.h:717
static const KeyPair *const end

◆ clear()

void QFontCache::clear ( )

Definition at line 2981 of file qfont.cpp.

Referenced by QFontDatabasePrivate::invalidate(), removeEngineForFont(), and ~QFontCache().

2982 {
2983  {
2985  end = engineDataCache.end();
2986  while (it != end) {
2987  QFontEngineData *data = it.value();
2988 #if !defined(Q_WS_MAC)
2989  for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
2990  if (data->engines[i]) {
2991  data->engines[i]->ref.deref();
2992  data->engines[i] = 0;
2993  }
2994  }
2995 #else
2996  if (data->engine) {
2997  data->engine->ref.deref();
2998  data->engine = 0;
2999  }
3000 #endif
3001  ++it;
3002  }
3003  }
3004 
3006  it != end; ++it) {
3007  if (it->data->ref == 0) {
3008  delete it->data;
3009  it->data = 0;
3010  }
3011  }
3012 
3014  it != end; ++it) {
3015  if (it->data && it->data->ref == 0) {
3016  delete it->data;
3017  it->data = 0;
3018  }
3019  }
3020 
3021  engineCache.clear();
3022 }
QAtomicInt ref
#define it(className, varName)
EngineCache engineCache
Definition: qfont_p.h:267
bool deref()
Atomically decrements the value of this QAtomicInt.
iterator Iterator
Qt-style synonym for QMap::iterator.
Definition: qmap.h:388
static const char * data(const QByteArray &arr)
EngineDataCache engineDataCache
Definition: qfont_p.h:251
QAtomicInt ref
Definition: qfont_p.h:152
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
QFontEngine * engine
Definition: qfont_p.h:158
static const KeyPair *const end
void clear()
Removes all items from the map.
Definition: qmap.h:444

◆ decreaseCost()

void QFontCache::decreaseCost ( uint  cost)
private

Definition at line 3111 of file qfont.cpp.

Referenced by cleanupPrinterFonts(), and timerEvent().

3112 {
3113  cost = (cost + 512) / 1024; // cost is stored in kb
3114  cost = cost > 0 ? cost : 1;
3115  Q_ASSERT(cost <= total_cost);
3116  total_cost -= cost;
3117 
3118  FC_DEBUG(" COST: decreased %u kb, total_cost %u kb, max_cost %u kb",
3119  cost, total_cost, max_cost);
3120 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define FC_DEBUG
Definition: qfont.cpp:89
uint total_cost
Definition: qfont_p.h:282
uint max_cost
Definition: qfont_p.h:282

◆ findEngine()

QFontEngine * QFontCache::findEngine ( const Key key)

Definition at line 3054 of file qfont.cpp.

Referenced by QFontDatabase::findFont(), QFontDatabase::load(), QFontEngineMultiFT::loadEngine(), loadSingleEngine(), loadWin(), QFontDatabase::loadXlfd(), and qt_load_font_engine_win().

3055 {
3057  end = engineCache.end();
3058  if (it == end) return 0;
3059 
3060  // found... update the hitcount and timestamp
3061  it.value().hits++;
3062  it.value().timestamp = ++current_timestamp;
3063 
3064  FC_DEBUG("QFontCache: found font engine\n"
3065  " %p: timestamp %4u hits %3u ref %2d/%2d, type '%s'",
3066  it.value().data, it.value().timestamp, it.value().hits,
3067  int(it.value().data->ref), it.value().data->cache_count,
3068  it.value().data->name());
3069 
3070  return it.value().data;
3071 }
#define it(className, varName)
EngineCache engineCache
Definition: qfont_p.h:267
iterator find(const Key &key)
Returns an iterator pointing to the item with key key in the map.
Definition: qmap.h:618
#define FC_DEBUG
Definition: qfont.cpp:89
iterator Iterator
Qt-style synonym for QMap::iterator.
Definition: qmap.h:388
uint current_timestamp
Definition: qfont_p.h:283
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
int key
static const KeyPair *const end

◆ findEngineData()

QFontEngineData * QFontCache::findEngineData ( const Key key) const

Definition at line 3036 of file qfont.cpp.

Referenced by getEngineData(), QFontDatabase::load(), and registerFont().

3037 {
3039  end = engineDataCache.end();
3040  if (it == end) return 0;
3041 
3042  // found
3043  return it.value();
3044 }
#define it(className, varName)
iterator find(const Key &key)
Returns an iterator pointing to the item with key key in the map.
Definition: qmap.h:618
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qmap.h:251
const_iterator ConstIterator
Qt-style synonym for QMap::const_iterator.
Definition: qmap.h:389
EngineDataCache engineDataCache
Definition: qfont_p.h:251
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
int key
static const KeyPair *const end

◆ increaseCost()

void QFontCache::increaseCost ( uint  cost)
private

Definition at line 3089 of file qfont.cpp.

Referenced by insertEngine(), and insertEngineData().

3090 {
3091  cost = (cost + 512) / 1024; // store cost in kb
3092  cost = cost > 0 ? cost : 1;
3093  total_cost += cost;
3094 
3095  FC_DEBUG(" COST: increased %u kb, total_cost %u kb, max_cost %u kb",
3096  cost, total_cost, max_cost);
3097 
3098  if (total_cost > max_cost) {
3099  max_cost = total_cost;
3100 
3101  if (timer_id == -1 || ! fast) {
3102  FC_DEBUG(" TIMER: starting fast timer (%d ms)", fast_timeout);
3103 
3104  if (timer_id != -1) killTimer(timer_id);
3106  fast = true;
3107  }
3108  }
3109 }
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
#define FC_DEBUG
Definition: qfont.cpp:89
uint total_cost
Definition: qfont_p.h:282
uint max_cost
Definition: qfont_p.h:282
static const int fast_timeout
Definition: qfont.cpp:2899
int timer_id
Definition: qfont_p.h:285
bool fast
Definition: qfont_p.h:284
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ insertEngine()

void QFontCache::insertEngine ( const Key key,
QFontEngine engine 
)

Definition at line 3073 of file qfont.cpp.

Referenced by QFontDatabase::load(), QFontEngineMultiFT::loadEngine(), loadSingleEngine(), loadWin(), QFontDatabase::loadXlfd(), and registerFont().

3074 {
3075  FC_DEBUG("QFontCache: inserting new engine %p", engine);
3076 
3077  Engine data(engine);
3078  data.timestamp = ++current_timestamp;
3079 
3081 
3082  // only increase the cost if this is the first time we insert the engine
3083  if (engine->cache_count == 0)
3084  increaseCost(engine->cache_cost);
3085 
3086  ++engine->cache_count;
3087 }
EngineCache engineCache
Definition: qfont_p.h:267
#define FC_DEBUG
Definition: qfont.cpp:89
static const char * data(const QByteArray &arr)
uint current_timestamp
Definition: qfont_p.h:283
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
int key
void increaseCost(uint cost)
Definition: qfont.cpp:3089

◆ insertEngineData()

void QFontCache::insertEngineData ( const Key key,
QFontEngineData engineData 
)

Definition at line 3046 of file qfont.cpp.

Referenced by getEngineData(), QFontDatabase::load(), and registerFont().

3047 {
3048  FC_DEBUG("QFontCache: inserting new engine data %p", engineData);
3049 
3050  engineDataCache.insert(key, engineData);
3051  increaseCost(sizeof(QFontEngineData));
3052 }
#define FC_DEBUG
Definition: qfont.cpp:89
EngineDataCache engineDataCache
Definition: qfont_p.h:251
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
int key
void increaseCost(uint cost)
Definition: qfont.cpp:3089

◆ instance()

QFontCache * QFontCache::instance ( )
static

Definition at line 2919 of file qfont.cpp.

Referenced by QFontPrivate::engineForScript(), QFontDatabase::findFont(), getEngineData(), QFontDatabasePrivate::invalidate(), QFontDatabase::load(), QFontEngineMultiFT::loadEngine(), loadSingleEngine(), loadWin(), QFontDatabase::loadXlfd(), qt_load_font_engine_win(), QApplication::qwsProcessEvent(), and registerFont().

2920 {
2921  QFontCache *&fontCache = theFontCache()->localData();
2922  if (!fontCache)
2923  fontCache = new QFontCache;
2924  return fontCache;
2925 }
QFontCache()
Definition: qfont.cpp:2940

◆ removeEngineForFont()

void QFontCache::removeEngineForFont ( const QByteArray fontName)

Definition at line 3025 of file qfont.cpp.

Referenced by QApplication::qwsProcessEvent().

3026 {
3027 
3028  /* This could be optimized but the code becomes much more complex if we want to handle multi
3029  * font engines and it is probably not worth it. Therefore we just clear the entire font cache.
3030  */
3031  Q_UNUSED(_fontName);
3032  clear();
3033 }
void clear()
Definition: qfont.cpp:2981
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ timerEvent()

void QFontCache::timerEvent ( QTimerEvent event)
privatevirtual

This event handler can be reimplemented in a subclass to receive timer events for the object.

QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the event parameter.

See also
startTimer(), killTimer(), event()

Reimplemented from QObject.

Definition at line 3186 of file qfont.cpp.

3187 {
3188  FC_DEBUG("QFontCache::timerEvent: performing cache maintenance (timestamp %u)",
3190 
3191  if (total_cost <= max_cost && max_cost <= min_cost) {
3192  FC_DEBUG(" cache redused sufficiently, stopping timer");
3193 
3195  timer_id = -1;
3196  fast = false;
3197 
3198  return;
3199  }
3200 
3201  // go through the cache and count up everything in use
3202  uint in_use_cost = 0;
3203 
3204  {
3205  FC_DEBUG(" SWEEP engine data:");
3206 
3207  // make sure the cost of each engine data is at least 1kb
3208  const uint engine_data_cost =
3209  sizeof(QFontEngineData) > 1024 ? sizeof(QFontEngineData) : 1024;
3210 
3213  for (; it != end; ++it) {
3214 #ifdef QFONTCACHE_DEBUG
3215  FC_DEBUG(" %p: ref %2d", it.value(), int(it.value()->ref));
3216 
3217 # if defined(Q_WS_X11) || defined(Q_WS_WIN)
3218  // print out all engines
3219  for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
3220  if (! it.value()->engines[i])
3221  continue;
3222  FC_DEBUG(" contains %p", it.value()->engines[i]);
3223  }
3224 # endif // Q_WS_X11 || Q_WS_WIN
3225 #endif // QFONTCACHE_DEBUG
3226 
3227  if (it.value()->ref != 0)
3228  in_use_cost += engine_data_cost;
3229  }
3230  }
3231 
3232  {
3233  FC_DEBUG(" SWEEP engine:");
3234 
3236  end = engineCache.constEnd();
3237  for (; it != end; ++it) {
3238  FC_DEBUG(" %p: timestamp %4u hits %2u ref %2d/%2d, cost %u bytes",
3239  it.value().data, it.value().timestamp, it.value().hits,
3240  int(it.value().data->ref), it.value().data->cache_count,
3241  it.value().data->cache_cost);
3242 
3243  if (it.value().data->ref != 0)
3244  in_use_cost += it.value().data->cache_cost / it.value().data->cache_count;
3245  }
3246 
3247  // attempt to make up for rounding errors
3248  in_use_cost += engineCache.size();
3249  }
3250 
3251  in_use_cost = (in_use_cost + 512) / 1024; // cost is stored in kb
3252 
3253  /*
3254  calculate the new maximum cost for the cache
3255 
3256  NOTE: in_use_cost is *not* correct due to rounding errors in the
3257  above algorithm. instead of worrying about getting the
3258  calculation correct, we are more interested in speed, and use
3259  in_use_cost as a floor for new_max_cost
3260  */
3261  uint new_max_cost = qMax(qMax(max_cost / 2, in_use_cost), min_cost);
3262 
3263  FC_DEBUG(" after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb",
3264  in_use_cost, total_cost, max_cost, new_max_cost);
3265 
3266  if (new_max_cost == max_cost) {
3267  if (fast) {
3268  FC_DEBUG(" cannot shrink cache, slowing timer");
3269 
3272  fast = false;
3273  }
3274 
3275  return;
3276  } else if (! fast) {
3277  FC_DEBUG(" dropping into passing gear");
3278 
3281  fast = true;
3282  }
3283 
3284  max_cost = new_max_cost;
3285 
3286  {
3287  FC_DEBUG(" CLEAN engine data:");
3288 
3289  // clean out all unused engine data
3291  end = engineDataCache.end();
3292  while (it != end) {
3293  if (it.value()->ref != 0) {
3294  ++it;
3295  continue;
3296  }
3297 
3298  EngineDataCache::Iterator rem = it++;
3299 
3300  decreaseCost(sizeof(QFontEngineData));
3301 
3302  FC_DEBUG(" %p", rem.value());
3303 
3304  delete rem.value();
3305  engineDataCache.erase(rem);
3306  }
3307  }
3308 
3309  // clean out the engine cache just enough to get below our new max cost
3310  uint current_cost;
3311  do {
3312  current_cost = total_cost;
3313 
3315  end = engineCache.end();
3316  // determine the oldest and least popular of the unused engines
3317  uint oldest = ~0u;
3318  uint least_popular = ~0u;
3319 
3320  for (; it != end; ++it) {
3321  if (it.value().data->ref != 0)
3322  continue;
3323 
3324  if (it.value().timestamp < oldest &&
3325  it.value().hits <= least_popular) {
3326  oldest = it.value().timestamp;
3327  least_popular = it.value().hits;
3328  }
3329  }
3330 
3331  FC_DEBUG(" oldest %u least popular %u", oldest, least_popular);
3332 
3333  for (it = engineCache.begin(); it != end; ++it) {
3334  if (it.value().data->ref == 0 &&
3335  it.value().timestamp == oldest &&
3336  it.value().hits == least_popular)
3337  break;
3338  }
3339 
3340  if (it != end) {
3341  FC_DEBUG(" %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'",
3342  it.value().data, it.value().timestamp, it.value().hits,
3343  int(it.value().data->ref), it.value().data->cache_count,
3344  it.value().data->name());
3345 
3346  if (--it.value().data->cache_count == 0) {
3347  FC_DEBUG(" DELETE: last occurrence in cache");
3348 
3349  decreaseCost(it.value().data->cache_cost);
3350  delete it.value().data;
3351  } else {
3352  /*
3353  this particular font engine is in the cache multiple
3354  times... set current_cost to zero, so that we can
3355  keep looping to get rid of all occurrences
3356  */
3357  current_cost = 0;
3358  }
3359 
3360  engineCache.erase(it);
3361  }
3362  } while (current_cost != total_cost && total_cost > max_cost);
3363 }
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
#define it(className, varName)
static const uint min_cost
Definition: qfont_p.h:281
EngineCache engineCache
Definition: qfont_p.h:267
int size() const
Returns the number of (key, value) pairs in the map.
Definition: qmap.h:201
void decreaseCost(uint cost)
Definition: qfont.cpp:3111
quint16 u
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define FC_DEBUG
Definition: qfont.cpp:89
const_iterator ConstIterator
Qt-style synonym for QMap::const_iterator.
Definition: qmap.h:389
iterator Iterator
Qt-style synonym for QMap::iterator.
Definition: qmap.h:388
unsigned int uint
Definition: qglobal.h:996
uint total_cost
Definition: qfont_p.h:282
EngineDataCache engineDataCache
Definition: qfont_p.h:251
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
static const int slow_timeout
Definition: qfont.cpp:2900
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
uint current_timestamp
Definition: qfont_p.h:283
uint max_cost
Definition: qfont_p.h:282
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
iterator erase(iterator it)
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to...
Definition: qmap.h:717
static const int fast_timeout
Definition: qfont.cpp:2899
int timer_id
Definition: qfont_p.h:285
static const KeyPair *const end
bool fast
Definition: qfont_p.h:284
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

Properties

◆ current_timestamp

uint QFontCache::current_timestamp
private

Definition at line 283 of file qfont_p.h.

Referenced by findEngine(), insertEngine(), and timerEvent().

◆ engineCache

EngineCache QFontCache::engineCache

Definition at line 267 of file qfont_p.h.

Referenced by cleanupPrinterFonts(), clear(), findEngine(), insertEngine(), timerEvent(), and ~QFontCache().

◆ engineDataCache

EngineDataCache QFontCache::engineDataCache

◆ fast

bool QFontCache::fast
private

Definition at line 284 of file qfont_p.h.

Referenced by increaseCost(), and timerEvent().

◆ max_cost

uint QFontCache::max_cost
private

Definition at line 282 of file qfont_p.h.

Referenced by decreaseCost(), increaseCost(), and timerEvent().

◆ min_cost

const uint QFontCache::min_cost = 4*1024
staticprivate

Definition at line 281 of file qfont_p.h.

Referenced by timerEvent().

◆ timer_id

int QFontCache::timer_id
private

Definition at line 285 of file qfont_p.h.

Referenced by increaseCost(), and timerEvent().

◆ total_cost

uint QFontCache::total_cost
private

Definition at line 282 of file qfont_p.h.

Referenced by decreaseCost(), increaseCost(), and timerEvent().


The documentation for this class was generated from the following files: