Fallback font match caching to fix emoji lag. (#7208)

This commit is contained in:
Gary Qian 2018-12-13 10:39:09 -08:00 committed by GitHub
parent 18a4e33c2a
commit eff5e67356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -202,6 +202,22 @@ std::shared_ptr<minikin::FontFamily> FontCollection::CreateMinikinFontFamily(
const std::shared_ptr<minikin::FontFamily>& FontCollection::MatchFallbackFont(
uint32_t ch,
std::string locale) {
// Check if the ch's matched font has been cached. We cache the results of
// this method as repeated matchFamilyStyleCharacter calls can become
// extremely laggy when typing a large number of complex emojis.
auto lookup = fallback_match_cache_.find(ch);
if (lookup != fallback_match_cache_.end()) {
return *lookup->second;
}
const std::shared_ptr<minikin::FontFamily>* match =
&DoMatchFallbackFont(ch, locale);
fallback_match_cache_.insert(std::make_pair(ch, match));
return *match;
}
const std::shared_ptr<minikin::FontFamily>& FontCollection::DoMatchFallbackFont(
uint32_t ch,
std::string locale) {
for (const sk_sp<SkFontMgr>& manager : GetFontManagerOrder()) {
std::vector<const char*> bcp47;
if (!locale.empty())
@ -219,7 +235,6 @@ const std::shared_ptr<minikin::FontFamily>& FontCollection::MatchFallbackFont(
return GetFallbackFontFamily(manager, family_name);
}
return g_null_family;
}

View File

@ -49,6 +49,8 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
const std::string& family,
const std::string& locale);
// Provides a FontFamily that contains glyphs for ch. This caches previously
// matched fonts. Also see FontCollection::DoMatchFallbackFont.
const std::shared_ptr<minikin::FontFamily>& MatchFallbackFont(
uint32_t ch,
std::string locale);
@ -80,12 +82,22 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
std::shared_ptr<minikin::FontCollection>,
FamilyKey::Hasher>
font_collections_cache_;
// Cache that stores the results of MatchFallbackFont to ensure lag-free emoji
// font fallback matching.
std::unordered_map<uint32_t, const std::shared_ptr<minikin::FontFamily>*>
fallback_match_cache_;
std::unordered_map<std::string, std::shared_ptr<minikin::FontFamily>>
fallback_fonts_;
std::unordered_map<std::string, std::set<std::string>>
fallback_fonts_for_locale_;
bool enable_font_fallback_;
// Performs the actual work of MatchFallbackFont. The result is cached in
// fallback_match_cache_.
const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFont(
uint32_t ch,
std::string locale);
std::vector<sk_sp<SkFontMgr>> GetFontManagerOrder() const;
std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamily(