| 629 | } |
| 630 | |
| 631 | void Tr2FontManager::TrimGlyphCache( size_t cacheSize ) |
| 632 | { |
| 633 | if( cacheSize >= m_totalGlyphsCachedSize ) |
| 634 | { |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | if( cacheSize == 0 ) |
| 639 | { |
| 640 | ClearCachedGlyphs(); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | struct Entry |
| 645 | { |
| 646 | unsigned int age; |
| 647 | FTC_SBit sbit; |
| 648 | |
| 649 | bool operator<( const Entry& other ) const |
| 650 | { |
| 651 | return age < other.age; |
| 652 | } |
| 653 | }; |
| 654 | |
| 655 | // Set up a list with age of all entries |
| 656 | std::vector<Entry> entries; |
| 657 | entries.reserve( m_sbitToCachedTextureMap.size() ); |
| 658 | |
| 659 | unsigned int now = unsigned( Tr2Renderer::GetCurrentFrameCounter() ); |
| 660 | for( auto it = m_sbitToCachedTextureMap.begin(); it != m_sbitToCachedTextureMap.end(); ++it ) |
| 661 | { |
| 662 | GlyphCacheEntry& gcEntry = it->second; |
| 663 | |
| 664 | Entry entry; |
| 665 | entry.age = now - gcEntry.lastFrameUsed; |
| 666 | entry.sbit = it->first; |
| 667 | |
| 668 | entries.push_back( entry ); |
| 669 | } |
| 670 | |
| 671 | // Sort by age |
| 672 | std::sort( entries.begin(), entries.end() ); |
| 673 | |
| 674 | // Delete oldest entries until cache is trimmed to size |
| 675 | for( auto toDeleteIt = entries.rbegin(); toDeleteIt != entries.rend(); ++toDeleteIt ) |
| 676 | { |
| 677 | Entry& entry = *toDeleteIt; |
| 678 | |
| 679 | auto foundIt = m_sbitToCachedTextureMap.find( entry.sbit ); |
| 680 | CCP_ASSERT( foundIt != m_sbitToCachedTextureMap.end() ); |
| 681 | |
| 682 | GlyphCacheEntry& gcEntry = foundIt->second; |
| 683 | m_totalGlyphsCachedSize -= gcEntry.memoryUsage; |
| 684 | gcEntry.glyphObject->Unlock(); |
| 685 | |
| 686 | m_sbitToCachedTextureMap.erase( foundIt ); |
| 687 | |
| 688 | if( m_totalGlyphsCachedSize < cacheSize ) |
nothing calls this directly
no test coverage detected