This source file includes following definitions.
- get
- contains
- release
- markAllVerticalData
- purge
- purgeLeastRecentlyUsed
#include "config.h"
#include "platform/fonts/FontDataCache.h"
#include "platform/fonts/SimpleFontData.h"
using namespace WTF;
namespace WebCore {
#if !OS(ANDROID)
const unsigned cMaxInactiveFontData = 250;
const unsigned cTargetInactiveFontData = 200;
#else
const unsigned cMaxInactiveFontData = 225;
const unsigned cTargetInactiveFontData = 200;
#endif
PassRefPtr<SimpleFontData> FontDataCache::get(const FontPlatformData* platformData, ShouldRetain shouldRetain)
{
if (!platformData)
return nullptr;
Cache::iterator result = m_cache.find(*platformData);
if (result == m_cache.end()) {
pair<RefPtr<SimpleFontData>, unsigned> newValue(SimpleFontData::create(*platformData), shouldRetain == Retain ? 1 : 0);
m_cache.set(*platformData, newValue);
if (shouldRetain == DoNotRetain)
m_inactiveFontData.add(newValue.first);
return newValue.first.release();
}
if (!result.get()->value.second) {
ASSERT(m_inactiveFontData.contains(result.get()->value.first));
m_inactiveFontData.remove(result.get()->value.first);
}
if (shouldRetain == Retain) {
result.get()->value.second++;
} else if (!result.get()->value.second) {
m_inactiveFontData.add(result.get()->value.first);
}
return result.get()->value.first;
}
bool FontDataCache::contains(const FontPlatformData* fontPlatformData) const
{
return m_cache.contains(*fontPlatformData);
}
void FontDataCache::release(const SimpleFontData* fontData)
{
ASSERT(!fontData->isCustomFont());
Cache::iterator it = m_cache.find(fontData->platformData());
ASSERT(it != m_cache.end());
if (it == m_cache.end())
return;
ASSERT(it->value.second);
if (!--it->value.second)
m_inactiveFontData.add(it->value.first);
}
void FontDataCache::markAllVerticalData()
{
#if ENABLE(OPENTYPE_VERTICAL)
Cache::iterator end = m_cache.end();
for (Cache::iterator fontData = m_cache.begin(); fontData != end; ++fontData) {
OpenTypeVerticalData* verticalData = const_cast<OpenTypeVerticalData*>(fontData->value.first->verticalData());
if (verticalData)
verticalData->setInFontCache(true);
}
#endif
}
bool FontDataCache::purge(PurgeSeverity PurgeSeverity)
{
if (PurgeSeverity == ForcePurge)
return purgeLeastRecentlyUsed(INT_MAX);
if (m_inactiveFontData.size() > cMaxInactiveFontData)
return purgeLeastRecentlyUsed(m_inactiveFontData.size() - cTargetInactiveFontData);
return false;
}
bool FontDataCache::purgeLeastRecentlyUsed(int count)
{
static bool isPurging;
if (isPurging)
return false;
isPurging = true;
Vector<RefPtr<SimpleFontData>, 20> fontDataToDelete;
ListHashSet<RefPtr<SimpleFontData> >::iterator end = m_inactiveFontData.end();
ListHashSet<RefPtr<SimpleFontData> >::iterator it = m_inactiveFontData.begin();
for (int i = 0; i < count && it != end; ++it, ++i) {
RefPtr<SimpleFontData>& fontData = *it.get();
m_cache.remove(fontData->platformData());
fontDataToDelete.append(fontData);
}
if (it == end) {
m_inactiveFontData.clear();
} else {
for (int i = 0; i < count; ++i)
m_inactiveFontData.remove(m_inactiveFontData.begin());
}
bool didWork = fontDataToDelete.size();
fontDataToDelete.clear();
isPurging = false;
return didWork;
}
}