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
This commit is contained in:
Seigo Nonaka
2016-02-03 19:41:00 +09:00
parent e7f39cb730
commit 109b667554
12 changed files with 144 additions and 220 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -24,7 +24,7 @@ minikin_src_files := \
FontLanguage.cpp \
FontLanguageListCache.cpp \
GraphemeBreak.cpp \
HbFaceCache.cpp \
HbFontCache.cpp \
Hyphenator.cpp \
Layout.cpp \
LayoutUtils.cpp \

View File

@@ -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

View File

@@ -28,7 +28,7 @@
#include "FontLanguage.h"
#include "FontLanguageListCache.h"
#include "HbFaceCache.h"
#include "HbFontCache.h"
#include "MinikinInternal.h"
#include <minikin/MinikinFont.h>
#include <minikin/AnalyzeStyle.h>
@@ -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

View File

@@ -16,10 +16,11 @@
#define LOG_TAG "Minikin"
#include "HbFaceCache.h"
#include "HbFontCache.h"
#include <cutils/log.h>
#include <hb.h>
#include <hb-ot.h>
#include <utils/LruCache.h>
#include <minikin/MinikinFont.h>
@@ -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<int32_t, hb_face_t*> {
class HbFontCache : private OnEntryRemoved<int32_t, hb_font_t*> {
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<int32_t, hb_face_t*> mCache;
LruCache<int32_t, hb_font_t*> 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

View File

@@ -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

View File

@@ -37,7 +37,7 @@
#include "FontLanguage.h"
#include "FontLanguageListCache.h"
#include "LayoutUtils.h"
#include "HbFaceCache.h"
#include "HbFontCache.h"
#include "MinikinInternal.h"
#include <minikin/MinikinFontFreeType.h>
#include <minikin/Layout.h>
@@ -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

View File

@@ -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

View File

@@ -378,18 +378,4 @@ TEST_F(FontFamilyTest, hasVariationSelectorTest) {
expectVSGlyphs(&family, kNotSupportedChar, std::set<uint32_t>());
}
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

View File

@@ -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 <gtest/gtest.h>
#include "HbFaceCache.h"
#include <cutils/log.h>
#include <hb.h>
#include <utils/Mutex.h>
#include "MinikinInternal.h"
#include <minikin/MinikinFont.h>
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

View File

@@ -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 <gtest/gtest.h>
#include "HbFontCache.h"
#include <cutils/log.h>
#include <hb.h>
#include <utils/Mutex.h>
#include "MinikinInternal.h"
#include "MinikinFontForTest.h"
#include <minikin/MinikinFont.h>
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