Skip to content

Commit

Permalink
libtxt: use a fixed set of font manager roles instead of a list of fo…
Browse files Browse the repository at this point in the history
…nt managers (flutter#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.
  • Loading branch information
jason-simmons authored Mar 8, 2018
1 parent bdd3a0b commit 6961f99
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/ui/text/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,7 +106,7 @@ void FontCollection::RegisterFontsFromAssetProvider(
}
}

collection_->PushFront(
collection_->SetAssetFontManager(
sk_make_sp<txt::AssetFontManager>(std::move(font_asset_data_provider)));
}

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion third_party/txt/benchmarks/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 23 additions & 15 deletions third_party/txt/src/txt/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions third_party/txt/src/txt/font_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -53,14 +53,18 @@ 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>>
fallback_fonts_;
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);

Expand Down
2 changes: 1 addition & 1 deletion third_party/txt/tests/render_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}

Expand Down

0 comments on commit 6961f99

Please sign in to comment.