libtxt: use a fixed set of font manager roles instead of a list of font managers (flutter/engine#4756)

Prior to this, the engine was adding font managers to the list every time the
app restarts (e.g. after a suspend-and-resume cycle), causing a leak.
This commit is contained in:
Jason Simmons 2018-03-08 12:40:08 -08:00 committed by GitHub
parent 64cfbcc419
commit 3e5ea529b5
5 changed files with 36 additions and 24 deletions

View File

@ -25,7 +25,7 @@ FontCollection& FontCollection::ForProcess() {
FontCollection::FontCollection()
: collection_(std::make_shared<txt::FontCollection>()) {
collection_->PushBack(SkFontMgr::RefDefault());
collection_->SetDefaultFontManager(SkFontMgr::RefDefault());
}
FontCollection::~FontCollection() = default;
@ -106,7 +106,7 @@ void FontCollection::RegisterFontsFromAssetProvider(
}
}
collection_->PushFront(
collection_->SetAssetFontManager(
sk_make_sp<txt::AssetFontManager>(std::move(font_asset_data_provider)));
}
@ -120,7 +120,7 @@ void FontCollection::RegisterTestFonts() {
asset_data_provider->RegisterTypeface(std::move(test_typeface),
GetTestFontFamilyName());
collection_->PushFront(sk_make_sp<txt::TestFontManager>(
collection_->SetTestFontManager(sk_make_sp<txt::TestFontManager>(
std::move(asset_data_provider), GetTestFontFamilyName()));
collection_->DisableFontFallback();

View File

@ -44,7 +44,7 @@ void SetCommandLine(fxl::CommandLine cmd) {
std::shared_ptr<FontCollection> GetTestFontCollection() {
auto collection = std::make_shared<FontCollection>();
collection->PushBack(sk_make_sp<AssetFontManager>(
collection->SetAssetFontManager(sk_make_sp<AssetFontManager>(
std::make_unique<DirectoryAssetDataProvider>(GetFontDir())));
return collection;
}

View File

@ -60,23 +60,31 @@ FontCollection::FontCollection() : enable_font_fallback_(true) {}
FontCollection::~FontCollection() = default;
size_t FontCollection::GetFontManagersCount() const {
return skia_font_managers_.size();
return GetFontManagerOrder().size();
}
void FontCollection::PushFront(sk_sp<SkFontMgr> skia_font_manager) {
if (!skia_font_manager) {
return;
}
UpdateFallbackFonts(skia_font_manager);
skia_font_managers_.push_front(std::move(skia_font_manager));
void FontCollection::SetDefaultFontManager(sk_sp<SkFontMgr> font_manager) {
default_font_manager_ = font_manager;
}
void FontCollection::PushBack(sk_sp<SkFontMgr> skia_font_manager) {
if (!skia_font_manager) {
return;
}
UpdateFallbackFonts(skia_font_manager);
skia_font_managers_.push_back(std::move(skia_font_manager));
void FontCollection::SetAssetFontManager(sk_sp<SkFontMgr> font_manager) {
asset_font_manager_ = font_manager;
}
void FontCollection::SetTestFontManager(sk_sp<SkFontMgr> font_manager) {
test_font_manager_ = font_manager;
}
// Return the available font managers in the order they should be queried.
std::vector<sk_sp<SkFontMgr>> FontCollection::GetFontManagerOrder() const {
std::vector<sk_sp<SkFontMgr>> order;
if (test_font_manager_)
order.push_back(test_font_manager_);
if (asset_font_manager_)
order.push_back(asset_font_manager_);
if (default_font_manager_)
order.push_back(default_font_manager_);
return order;
}
void FontCollection::DisableFontFallback() {
@ -91,7 +99,7 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) {
return cached->second;
}
for (sk_sp<SkFontMgr> manager : skia_font_managers_) {
for (sk_sp<SkFontMgr>& manager : GetFontManagerOrder()) {
auto font_style_set = manager->matchFamily(family.c_str());
if (font_style_set == nullptr || font_style_set->count() == 0) {
continue;
@ -157,7 +165,7 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) {
const std::shared_ptr<minikin::FontFamily>& FontCollection::MatchFallbackFont(
uint32_t ch) {
for (const auto& manager : skia_font_managers_) {
for (const sk_sp<SkFontMgr>& manager : GetFontManagerOrder()) {
sk_sp<SkTypeface> typeface(
manager->matchFamilyStyleCharacter(0, SkFontStyle(), nullptr, 0, ch));
if (!typeface)

View File

@ -39,9 +39,9 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
size_t GetFontManagersCount() const;
void PushFront(sk_sp<SkFontMgr> skia_font_manager);
void PushBack(sk_sp<SkFontMgr> skia_font_manager);
void SetDefaultFontManager(sk_sp<SkFontMgr> font_manager);
void SetAssetFontManager(sk_sp<SkFontMgr> font_manager);
void SetTestFontManager(sk_sp<SkFontMgr> font_manager);
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
const std::string& family);
@ -53,7 +53,9 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
void DisableFontFallback();
private:
std::deque<sk_sp<SkFontMgr>> skia_font_managers_;
sk_sp<SkFontMgr> default_font_manager_;
sk_sp<SkFontMgr> asset_font_manager_;
sk_sp<SkFontMgr> test_font_manager_;
std::unordered_map<std::string, std::shared_ptr<minikin::FontCollection>>
font_collections_cache_;
std::unordered_map<SkFontID, std::shared_ptr<minikin::FontFamily>>
@ -61,6 +63,8 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
std::shared_ptr<minikin::FontFamily> null_family_;
bool enable_font_fallback_;
std::vector<sk_sp<SkFontMgr>> GetFontManagerOrder() const;
const std::shared_ptr<minikin::FontFamily>& GetFontFamilyForTypeface(
const sk_sp<SkTypeface>& typeface);

View File

@ -30,7 +30,7 @@ namespace txt {
RenderTest::RenderTest()
: snapshots_(0), font_collection_(std::make_shared<FontCollection>()) {
font_collection_->PushBack(sk_make_sp<AssetFontManager>(
font_collection_->SetAssetFontManager(sk_make_sp<AssetFontManager>(
std::make_unique<txt::DirectoryAssetDataProvider>(GetFontDir())));
}