| 748 | } |
| 749 | |
| 750 | CacheGlyph* AddGlyphToCache(HFontMap font_map, uint32_t frame, uint64_t glyph_key, FontGlyph* glyph, int32_t g_offset_y) |
| 751 | { |
| 752 | DM_MUTEX_SCOPED_LOCK(font_map->m_Mutex); |
| 753 | |
| 754 | // Since accessing glyphs will update their timestamps, we need to sort them when |
| 755 | // we need to allocate a new glyph, so that we pick the oldest one |
| 756 | SortCache(font_map); |
| 757 | //DebugCache(font_map); |
| 758 | |
| 759 | // Locate a cache cell candidate |
| 760 | CacheGlyph* cache_glyph = AcquireFreeGlyphFromCache(font_map); |
| 761 | |
| 762 | if (cache_glyph->m_Glyph) |
| 763 | { |
| 764 | if (cache_glyph->m_Frame == frame) |
| 765 | { |
| 766 | bool can_resize = CanCacheTextureGrow(font_map); |
| 767 | if (can_resize) |
| 768 | { |
| 769 | font_map->m_IsCacheSizeDirty = 1; |
| 770 | font_map->m_IsCacheSizeTooSmall = 1; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | // It means we've filled the entire cache with upload requests |
| 775 | // We might then just as well skip the next uploads until the next frame |
| 776 | dmLogWarning("Entire font glyph cache (%u x %u) is filled in a single frame %u ('%c' %u / %u). Consider increasing the cache for %s", font_map->m_CacheWidth, font_map->m_CacheHeight, frame, glyph->m_Codepoint < 255 ? glyph->m_Codepoint : ' ', glyph->m_Codepoint, glyph->m_GlyphIndex , dmHashReverseSafe64(font_map->m_NameHash)); |
| 777 | return 0; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | if (cache_glyph->m_Glyph) // It already existed in the cache |
| 782 | { |
| 783 | // Clear the old data from the cache |
| 784 | font_map->m_GlyphCache.Erase(cache_glyph->m_GlyphKey); |
| 785 | } |
| 786 | |
| 787 | // If the blit would write outside of the texture, then we try to resize it |
| 788 | uint32_t glyph_image_width = glyph->m_Bitmap.m_Width; |
| 789 | uint32_t glyph_image_height = glyph->m_Bitmap.m_Height; |
| 790 | if ( (cache_glyph->m_X + glyph_image_width) > font_map->m_CacheWidth || |
| 791 | (cache_glyph->m_Y + glyph_image_height + g_offset_y) > font_map->m_CacheHeight) |
| 792 | { |
| 793 | bool can_resize = CanCacheTextureGrow(font_map); |
| 794 | if (can_resize) |
| 795 | { |
| 796 | font_map->m_IsCacheSizeDirty = 1; |
| 797 | font_map->m_IsCacheSizeTooSmall = 1; |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | // It means we've filled the entire cache with upload requests |
| 802 | // We might then just as well skip the next uploads until the next frame |
| 803 | dmLogWarning("The font glyph cache (%u x %u) needed resizing to fit glyph bitmap.", font_map->m_CacheWidth, font_map->m_CacheHeight); |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | cache_glyph->m_Glyph = glyph; |
no test coverage detected