From 109b6675541d10cfccfb188f94eb9def9d1d3723 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Wed, 3 Feb 2016 19:41:00 +0900 Subject: [PATCH] Improve Paint.hasGlyph performance by caching hb_font_t It turned out that hb_font_t creation is not a lightweight operation. Especially, Paint.hasGlyph creates hb_font_t for all existing fonts every time. To improve the performance, cache hb_font_t instead of hb_face_t. Note that to calculate horizontal advance, MinikinPaint needs to be associated with hb_font_t by calling hb_font_set_funcs. With this patch, hb_font_set_funcs may be called multiple times for the same hb_font_t object. However this is not an issue since MinikinPaint is unique during layout. Bug: 26784699 Change-Id: I516498ae9f0127d700fc9829327e9789845a1416 --- .../flutter/include/minikin/FontCollection.h | 3 - .../src/flutter/include/minikin/FontFamily.h | 11 +- engine/src/flutter/libs/minikin/Android.mk | 2 +- .../flutter/libs/minikin/FontCollection.cpp | 7 -- .../src/flutter/libs/minikin/FontFamily.cpp | 22 +--- .../{HbFaceCache.cpp => HbFontCache.cpp} | 60 +++++---- .../minikin/{HbFaceCache.h => HbFontCache.h} | 12 +- engine/src/flutter/libs/minikin/Layout.cpp | 26 +--- engine/src/flutter/tests/Android.mk | 2 +- engine/src/flutter/tests/FontFamilyTest.cpp | 14 --- engine/src/flutter/tests/HbFaceCacheTest.cpp | 118 ------------------ engine/src/flutter/tests/HbFontCacheTest.cpp | 87 +++++++++++++ 12 files changed, 144 insertions(+), 220 deletions(-) rename engine/src/flutter/libs/minikin/{HbFaceCache.cpp => HbFontCache.cpp} (60%) rename engine/src/flutter/libs/minikin/{HbFaceCache.h => HbFontCache.h} (77%) delete mode 100644 engine/src/flutter/tests/HbFaceCacheTest.cpp create mode 100644 engine/src/flutter/tests/HbFontCacheTest.cpp diff --git a/engine/src/flutter/include/minikin/FontCollection.h b/engine/src/flutter/include/minikin/FontCollection.h index 294692fced..3a63c07948 100644 --- a/engine/src/flutter/include/minikin/FontCollection.h +++ b/engine/src/flutter/include/minikin/FontCollection.h @@ -53,9 +53,6 @@ public: uint32_t getId() const; - // Calls each managed font family's FontFamily::purgeHbFontCache method. - // Caller should acquire a lock before calling the method. - void purgeFontFamilyHbFontCache() const; private: static const int kLogCharsPerPage = 8; static const int kPageMask = (1 << kLogCharsPerPage) - 1; diff --git a/engine/src/flutter/include/minikin/FontFamily.h b/engine/src/flutter/include/minikin/FontFamily.h index aa2e0ac2e4..2b59160ffa 100644 --- a/engine/src/flutter/include/minikin/FontFamily.h +++ b/engine/src/flutter/include/minikin/FontFamily.h @@ -100,11 +100,11 @@ struct FakedFont { class FontFamily : public MinikinRefCounted { public: - FontFamily() : mHbFont(nullptr) { } + FontFamily() {} FontFamily(int variant); - FontFamily(uint32_t langId, int variant) : mLangId(langId), mVariant(variant), mHbFont(nullptr) { + FontFamily(uint32_t langId, int variant) : mLangId(langId), mVariant(variant) { } ~FontFamily(); @@ -131,11 +131,6 @@ public: // Caller should acquire a lock before calling the method. bool hasVariationSelector(uint32_t codepoint, uint32_t variationSelector); - // Purges cached mHbFont. - // hb_font_t keeps a reference to hb_face_t which is managed by HbFaceCache. Thus, - // it is good to purge hb_font_t once it is no longer necessary. - // Caller should acquire a lock before calling the method. - void purgeHbFontCache(); private: void addFontLocked(MinikinFont* typeface, FontStyle style); @@ -152,8 +147,6 @@ private: SparseBitSet mCoverage; bool mCoverageValid; - - hb_font_t* mHbFont; }; } // namespace android diff --git a/engine/src/flutter/libs/minikin/Android.mk b/engine/src/flutter/libs/minikin/Android.mk index 769b5fcd81..1fd0e6e8a0 100644 --- a/engine/src/flutter/libs/minikin/Android.mk +++ b/engine/src/flutter/libs/minikin/Android.mk @@ -24,7 +24,7 @@ minikin_src_files := \ FontLanguage.cpp \ FontLanguageListCache.cpp \ GraphemeBreak.cpp \ - HbFaceCache.cpp \ + HbFontCache.cpp \ Hyphenator.cpp \ Layout.cpp \ LayoutUtils.cpp \ diff --git a/engine/src/flutter/libs/minikin/FontCollection.cpp b/engine/src/flutter/libs/minikin/FontCollection.cpp index da58fa39c9..26aefe4490 100644 --- a/engine/src/flutter/libs/minikin/FontCollection.cpp +++ b/engine/src/flutter/libs/minikin/FontCollection.cpp @@ -410,11 +410,4 @@ uint32_t FontCollection::getId() const { return mId; } -void FontCollection::purgeFontFamilyHbFontCache() const { - assertMinikinLocked(); - for (size_t i = 0; i < mFamilies.size(); ++i) { - mFamilies[i]->purgeHbFontCache(); - } -} - } // namespace android diff --git a/engine/src/flutter/libs/minikin/FontFamily.cpp b/engine/src/flutter/libs/minikin/FontFamily.cpp index 1405789028..9eda3c2498 100644 --- a/engine/src/flutter/libs/minikin/FontFamily.cpp +++ b/engine/src/flutter/libs/minikin/FontFamily.cpp @@ -28,7 +28,7 @@ #include "FontLanguage.h" #include "FontLanguageListCache.h" -#include "HbFaceCache.h" +#include "HbFontCache.h" #include "MinikinInternal.h" #include #include @@ -189,23 +189,11 @@ const SparseBitSet* FontFamily::getCoverage() { bool FontFamily::hasVariationSelector(uint32_t codepoint, uint32_t variationSelector) { assertMinikinLocked(); - if (!mHbFont) { - const FontStyle defaultStyle; - MinikinFont* minikinFont = getClosestMatch(defaultStyle).font; - hb_face_t* face = getHbFaceLocked(minikinFont); - mHbFont = hb_font_create(face); - hb_ot_font_set_funcs(mHbFont); - } + const FontStyle defaultStyle; + MinikinFont* minikinFont = getClosestMatch(defaultStyle).font; + hb_font_t* font = getHbFontLocked(minikinFont); uint32_t unusedGlyph; - return hb_font_get_glyph(mHbFont, codepoint, variationSelector, &unusedGlyph); -} - -void FontFamily::purgeHbFontCache() { - assertMinikinLocked(); - if (mHbFont) { - hb_font_destroy(mHbFont); - mHbFont = nullptr; - } + return hb_font_get_glyph(font, codepoint, variationSelector, &unusedGlyph); } } // namespace android diff --git a/engine/src/flutter/libs/minikin/HbFaceCache.cpp b/engine/src/flutter/libs/minikin/HbFontCache.cpp similarity index 60% rename from engine/src/flutter/libs/minikin/HbFaceCache.cpp rename to engine/src/flutter/libs/minikin/HbFontCache.cpp index 235f7f1b0c..73308efab7 100644 --- a/engine/src/flutter/libs/minikin/HbFaceCache.cpp +++ b/engine/src/flutter/libs/minikin/HbFontCache.cpp @@ -16,10 +16,11 @@ #define LOG_TAG "Minikin" -#include "HbFaceCache.h" +#include "HbFontCache.h" #include #include +#include #include #include @@ -51,23 +52,23 @@ static hb_blob_t* referenceTable(hb_face_t* /* face */, hb_tag_t tag, void* user HB_MEMORY_MODE_WRITABLE, buffer, free); } -class HbFaceCache : private OnEntryRemoved { +class HbFontCache : private OnEntryRemoved { public: - HbFaceCache() : mCache(kMaxEntries) { + HbFontCache() : mCache(kMaxEntries) { mCache.setOnEntryRemovedListener(this); } // callback for OnEntryRemoved - void operator()(int32_t& /* key */, hb_face_t*& value) { - hb_face_destroy(value); + void operator()(int32_t& /* key */, hb_font_t*& value) { + hb_font_destroy(value); } - hb_face_t* get(int32_t fontId) { + hb_font_t* get(int32_t fontId) { return mCache.get(fontId); } - void put(int32_t fontId, hb_face_t* face) { - mCache.put(fontId, face); + void put(int32_t fontId, hb_font_t* font) { + mCache.put(fontId, font); } void clear() { @@ -77,39 +78,52 @@ public: private: static const size_t kMaxEntries = 100; - LruCache mCache; + LruCache mCache; }; -HbFaceCache* getFaceCacheLocked() { +HbFontCache* getFontCacheLocked() { assertMinikinLocked(); - static HbFaceCache* cache = nullptr; + static HbFontCache* cache = nullptr; if (cache == nullptr) { - cache = new HbFaceCache(); + cache = new HbFontCache(); } return cache; } -void purgeHbFaceCacheLocked() { +void purgeHbFontCacheLocked() { assertMinikinLocked(); - getFaceCacheLocked()->clear(); + getFontCacheLocked()->clear(); } -hb_face_t* getHbFaceLocked(MinikinFont* minikinFont) { +hb_font_t* getHbFontLocked(MinikinFont* minikinFont) { assertMinikinLocked(); + static hb_font_t* nullFaceFont = nullptr; if (minikinFont == nullptr) { - return nullptr; + if (nullFaceFont == nullptr) { + nullFaceFont = hb_font_create(nullptr); + } + return nullFaceFont; } - HbFaceCache* faceCache = getFaceCacheLocked(); + HbFontCache* fontCache = getFontCacheLocked(); const int32_t fontId = minikinFont->GetUniqueId(); - hb_face_t* face = faceCache->get(fontId); - if (face != nullptr) { - return face; + hb_font_t* font = fontCache->get(fontId); + if (font != nullptr) { + return font; } - face = hb_face_create_for_tables(referenceTable, minikinFont, nullptr); - faceCache->put(fontId, face); - return face; + hb_face_t* face = hb_face_create_for_tables(referenceTable, minikinFont, nullptr); + hb_font_t* parent_font = hb_font_create(face); + hb_ot_font_set_funcs(parent_font); + + unsigned int upem = hb_face_get_upem(face); + hb_font_set_scale(parent_font, upem, upem); + + font = hb_font_create_sub_font(parent_font); + hb_font_destroy(parent_font); + hb_face_destroy(face); + fontCache->put(fontId, font); + return font; } } // namespace android diff --git a/engine/src/flutter/libs/minikin/HbFaceCache.h b/engine/src/flutter/libs/minikin/HbFontCache.h similarity index 77% rename from engine/src/flutter/libs/minikin/HbFaceCache.h rename to engine/src/flutter/libs/minikin/HbFontCache.h index 8ee38c3603..62564d35b9 100644 --- a/engine/src/flutter/libs/minikin/HbFaceCache.h +++ b/engine/src/flutter/libs/minikin/HbFontCache.h @@ -14,16 +14,16 @@ * limitations under the License. */ -#ifndef MINIKIN_HBFACE_CACHE_H -#define MINIKIN_HBFACE_CACHE_H +#ifndef MINIKIN_HBFONT_CACHE_H +#define MINIKIN_HBFONT_CACHE_H -struct hb_face_t; +struct hb_font_t; namespace android { class MinikinFont; -void purgeHbFaceCacheLocked(); -hb_face_t* getHbFaceLocked(MinikinFont* minikinFont); +void purgeHbFontCacheLocked(); +hb_font_t* getHbFontLocked(MinikinFont* minikinFont); } // namespace android -#endif // MINIKIN_HBFACE_CACHE_H +#endif // MINIKIN_HBFONT_CACHE_H diff --git a/engine/src/flutter/libs/minikin/Layout.cpp b/engine/src/flutter/libs/minikin/Layout.cpp index 2839e5b71d..71e0d890b7 100644 --- a/engine/src/flutter/libs/minikin/Layout.cpp +++ b/engine/src/flutter/libs/minikin/Layout.cpp @@ -37,7 +37,7 @@ #include "FontLanguage.h" #include "FontLanguageListCache.h" #include "LayoutUtils.h" -#include "HbFaceCache.h" +#include "HbFontCache.h" #include "MinikinInternal.h" #include #include @@ -98,7 +98,7 @@ struct LayoutContext { void clearHbFonts() { for (size_t i = 0; i < hbFonts.size(); i++) { - hb_font_destroy(hbFonts[i]); + hb_font_set_funcs(hbFonts[i], nullptr, nullptr, nullptr); } hbFonts.clear(); } @@ -133,7 +133,6 @@ public: layout->setFontCollection(collection); layout->mAdvances.resize(mCount, 0); ctx->clearHbFonts(); - collection->purgeFontFamilyHbFontCache(); layout->doLayoutRun(mChars, mStart, mCount, mNchars, mIsRtl, ctx); } @@ -306,21 +305,6 @@ hb_font_funcs_t* getHbFontFuncs() { return hbFontFuncs; } -static hb_font_t* create_hb_font(MinikinFont* minikinFont, MinikinPaint* minikinPaint) { - hb_face_t* face = getHbFaceLocked(minikinFont); - hb_font_t* parent_font = hb_font_create(face); - hb_ot_font_set_funcs(parent_font); - - unsigned int upem = hb_face_get_upem(face); - hb_font_set_scale(parent_font, upem, upem); - - hb_font_t* font = hb_font_create_sub_font(parent_font); - hb_font_destroy(parent_font); - - hb_font_set_funcs(font, getHbFontFuncs(), minikinPaint, 0); - return font; -} - static float HBFixedToFloat(hb_position_t v) { return scalbnf (v, -8); @@ -349,7 +333,8 @@ int Layout::findFace(FakedFont face, LayoutContext* ctx) { // Note: ctx == NULL means we're copying from the cache, no need to create // corresponding hb_font object. if (ctx != NULL) { - hb_font_t* font = create_hb_font(face.font, &ctx->paint); + hb_font_t* font = getHbFontLocked(face.font); + hb_font_set_funcs(font, getHbFontFuncs(), &ctx->paint, 0); ctx->hbFonts.push_back(font); } return ix; @@ -599,7 +584,6 @@ void Layout::doLayout(const uint16_t* buf, size_t start, size_t count, size_t bu start, mCollection, this, NULL); } ctx.clearHbFonts(); - mCollection->purgeFontFamilyHbFontCache(); } float Layout::measureText(const uint16_t* buf, size_t start, size_t count, size_t bufSize, @@ -976,7 +960,7 @@ void Layout::purgeCaches() { AutoMutex _l(gMinikinLock); LayoutCache& layoutCache = LayoutEngine::getInstance().layoutCache; layoutCache.clear(); - purgeHbFaceCacheLocked(); + purgeHbFontCacheLocked(); } } // namespace android diff --git a/engine/src/flutter/tests/Android.mk b/engine/src/flutter/tests/Android.mk index 2483b763c4..d2aa5fd107 100644 --- a/engine/src/flutter/tests/Android.mk +++ b/engine/src/flutter/tests/Android.mk @@ -75,9 +75,9 @@ LOCAL_SRC_FILES += \ FontFamilyTest.cpp \ FontLanguageListCacheTest.cpp \ FontTestUtils.cpp \ + HbFontCacheTest.cpp \ MinikinFontForTest.cpp \ GraphemeBreakTests.cpp \ - HbFaceCacheTest.cpp \ LayoutUtilsTest.cpp \ UnicodeUtils.cpp diff --git a/engine/src/flutter/tests/FontFamilyTest.cpp b/engine/src/flutter/tests/FontFamilyTest.cpp index 6b32689081..fef464f1d7 100644 --- a/engine/src/flutter/tests/FontFamilyTest.cpp +++ b/engine/src/flutter/tests/FontFamilyTest.cpp @@ -378,18 +378,4 @@ TEST_F(FontFamilyTest, hasVariationSelectorTest) { expectVSGlyphs(&family, kNotSupportedChar, std::set()); } -TEST_F(FontFamilyTest, hasVariationSelectorWorksAfterpurgeHbFontCache) { - MinikinFontForTest minikinFont(kVsTestFont); - FontFamily family; - family.addFont(&minikinFont); - - const uint32_t kVS1 = 0xFE00; - const uint32_t kSupportedChar1 = 0x82A6; - - AutoMutex _l(gMinikinLock); - EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1)); - - family.purgeHbFontCache(); - EXPECT_TRUE(family.hasVariationSelector(kSupportedChar1, kVS1)); -} } // namespace android diff --git a/engine/src/flutter/tests/HbFaceCacheTest.cpp b/engine/src/flutter/tests/HbFaceCacheTest.cpp deleted file mode 100644 index 76eec5b5a8..0000000000 --- a/engine/src/flutter/tests/HbFaceCacheTest.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include "HbFaceCache.h" - -#include -#include -#include - -#include "MinikinInternal.h" -#include - -namespace android { -namespace { - -// A mock implementation of MinikinFont. The passed integer value will be -// returned in GetUniqueId(). -class MockMinikinFont : public MinikinFont { -public: - MockMinikinFont(int32_t id) : mId(id) { - } - - virtual float GetHorizontalAdvance(uint32_t /* glyph_id */, - const MinikinPaint& /* paint */) const { - LOG_ALWAYS_FATAL("MockMinikinFont::GetHorizontalAdvance is not implemented."); - return 0.0f; - } - - virtual void GetBounds(MinikinRect* /* bounds */, uint32_t /* glyph_id */, - const MinikinPaint& /* paint */) const { - LOG_ALWAYS_FATAL("MockMinikinFont::GetBounds is not implemented."); - } - - virtual bool GetTable(uint32_t /* tag */, uint8_t* /* buf */, size_t* /* size */) { - LOG_ALWAYS_FATAL("MockMinikinFont::GetTable is not implemented."); - return false; - } - - virtual int32_t GetUniqueId() const { - return mId; - } - -private: - int32_t mId; -}; - -class HbFaceCacheTest : public testing::Test { -public: - virtual void TearDown() { - AutoMutex _l(gMinikinLock); - purgeHbFaceCacheLocked(); - } -}; - -TEST_F(HbFaceCacheTest, getHbFaceLockedTest) { - AutoMutex _l(gMinikinLock); - - MockMinikinFont fontA(1); - MockMinikinFont fontB(2); - MockMinikinFont fontC(2); - - // Never return NULL. - EXPECT_TRUE(getHbFaceLocked(&fontA)); - EXPECT_TRUE(getHbFaceLocked(&fontB)); - EXPECT_TRUE(getHbFaceLocked(&fontC)); - - // Must return same object if same font object is passed. - EXPECT_EQ(getHbFaceLocked(&fontA), getHbFaceLocked(&fontA)); - EXPECT_EQ(getHbFaceLocked(&fontB), getHbFaceLocked(&fontB)); - EXPECT_EQ(getHbFaceLocked(&fontC), getHbFaceLocked(&fontC)); - - // Different object must be returned if the passed minikinFont has different ID. - EXPECT_NE(getHbFaceLocked(&fontA), getHbFaceLocked(&fontB)); - EXPECT_NE(getHbFaceLocked(&fontA), getHbFaceLocked(&fontC)); - - // Same object must be returned if the minikinFont has same Id. - EXPECT_EQ(getHbFaceLocked(&fontB), getHbFaceLocked(&fontC)); -} - -TEST_F(HbFaceCacheTest, purgeCacheTest) { - AutoMutex _l(gMinikinLock); - MockMinikinFont font(1); - - hb_face_t* face = getHbFaceLocked(&font); - EXPECT_TRUE(face); - - // Set user data to identify the face object. - hb_user_data_key_t key; - void* data = (void*)0xdeadbeef; - hb_face_set_user_data(face, &key, data, NULL, false); - EXPECT_EQ(data, hb_face_get_user_data(face, &key)); - - purgeHbFaceCacheLocked(); - - // By checking user data, confirm that the object after purge is different from previously - // created one. Do not compare the returned pointer here since memory allocator may assign - // same region for new object. - face = getHbFaceLocked(&font); - EXPECT_EQ(nullptr, hb_face_get_user_data(face, &key)); -} - -} // namespace -} // namespace android diff --git a/engine/src/flutter/tests/HbFontCacheTest.cpp b/engine/src/flutter/tests/HbFontCacheTest.cpp new file mode 100644 index 0000000000..2dee61aff0 --- /dev/null +++ b/engine/src/flutter/tests/HbFontCacheTest.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "HbFontCache.h" + +#include +#include +#include + +#include "MinikinInternal.h" +#include "MinikinFontForTest.h" +#include + +namespace android { +namespace { + +class HbFontCacheTest : public testing::Test { +public: + virtual void TearDown() { + AutoMutex _l(gMinikinLock); + purgeHbFontCacheLocked(); + } +}; + +TEST_F(HbFontCacheTest, getHbFontLockedTest) { + AutoMutex _l(gMinikinLock); + + MinikinFontForTest fontA(kTestFontDir "Regular.ttf"); + MinikinFontForTest fontB(kTestFontDir "Bold.ttf"); + MinikinFontForTest fontC(kTestFontDir "BoldItalic.ttf"); + + // Never return NULL. + EXPECT_NE(nullptr, getHbFontLocked(&fontA)); + EXPECT_NE(nullptr, getHbFontLocked(&fontB)); + EXPECT_NE(nullptr, getHbFontLocked(&fontC)); + + EXPECT_NE(nullptr, getHbFontLocked(nullptr)); + + // Must return same object if same font object is passed. + EXPECT_EQ(getHbFontLocked(&fontA), getHbFontLocked(&fontA)); + EXPECT_EQ(getHbFontLocked(&fontB), getHbFontLocked(&fontB)); + EXPECT_EQ(getHbFontLocked(&fontC), getHbFontLocked(&fontC)); + + // Different object must be returned if the passed minikinFont has different ID. + EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontB)); + EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontC)); +} + +TEST_F(HbFontCacheTest, purgeCacheTest) { + AutoMutex _l(gMinikinLock); + MinikinFontForTest minikinFont(kTestFontDir "Regular.ttf"); + + hb_font_t* font = getHbFontLocked(&minikinFont); + ASSERT_NE(nullptr, font); + + // Set user data to identify the font object. + hb_user_data_key_t key; + void* data = (void*)0xdeadbeef; + hb_font_set_user_data(font, &key, data, NULL, false); + ASSERT_EQ(data, hb_font_get_user_data(font, &key)); + + purgeHbFontCacheLocked(); + + // By checking user data, confirm that the object after purge is different from previously + // created one. Do not compare the returned pointer here since memory allocator may assign + // same region for new object. + font = getHbFontLocked(&minikinFont); + EXPECT_EQ(nullptr, hb_font_get_user_data(font, &key)); +} + +} // namespace +} // namespace android