Reduce memory usage of FontCollection.
This is 2nd attempt at I9e01d237c9adcb05e200932401cb1a4780049f86. The previous CL was reverted because 8-bit integers were too small to store the indices of mFamilyVec. This CL changes it to 16-bit integers since size_t is still unnecessary large. Theoretically, 32-bit integers are necessary for the indices of mFamilyVec since the size of mFamilyVec can be 0x10EE01. However, in practice, 16-bit integers are enough for the indices of mFamilyVec. The length of mFamilyVec for the system fonts is 2084. Even if the developers load their own very large fonts, it can only increase the number of elements in mFamilyVec to at most 0x10FF. As the result, memory usage of the FontCollections for the system fonts decreases as follows. 64-bit process: before: 398,264 bytes, after: 282,568 bytes (-115,696 bytes) 32-bit process: before: 199,132 bytes, after: 149,548 bytes (-49,584 bytes) Bug: 33562608 Test: Verified Emoji and CJK characters are present. Test: android.text.cts.EmojiTest passed Test: Minikin unit tests passed Change-Id: I6796fd55ac30fe30528a212ebf6097b1d672e2f8
This commit is contained in:
@@ -59,9 +59,14 @@ private:
|
||||
static const int kLogCharsPerPage = 8;
|
||||
static const int kPageMask = (1 << kLogCharsPerPage) - 1;
|
||||
|
||||
// mFamilyVec holds the indices of the mFamilies and mRanges holds the range of indices of
|
||||
// mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
|
||||
// the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
|
||||
// (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
|
||||
// only limited range of code points.
|
||||
struct Range {
|
||||
size_t start;
|
||||
size_t end;
|
||||
uint16_t start;
|
||||
uint16_t end;
|
||||
};
|
||||
|
||||
// Initialize the FontCollection.
|
||||
@@ -96,10 +101,12 @@ private:
|
||||
|
||||
// Following two vectors are pre-calculated tables for resolving coverage faster.
|
||||
// For example, to iterate over all fonts which support Unicode code point U+XXYYZZ,
|
||||
// iterate font families from mFamilyVec[mRanges[0xXXYY].start] to
|
||||
// iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to
|
||||
// mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies.
|
||||
// This vector contains indices into mFamilies.
|
||||
// This vector can't be empty.
|
||||
std::vector<Range> mRanges;
|
||||
std::vector<std::shared_ptr<FontFamily>> mFamilyVec;
|
||||
std::vector<uint8_t> mFamilyVec;
|
||||
|
||||
// This vector has pointers to the font family instances which have cmap 14 subtables.
|
||||
std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
|
||||
|
||||
@@ -118,8 +118,9 @@ void FontCollection::init(const vector<std::shared_ptr<FontFamily>>& typefaces)
|
||||
nTypefaces = mFamilies.size();
|
||||
LOG_ALWAYS_FATAL_IF(nTypefaces == 0,
|
||||
"Font collection must have at least one valid typeface");
|
||||
LOG_ALWAYS_FATAL_IF(nTypefaces > 254,
|
||||
"Font collection may only have up to 254 font families.");
|
||||
size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
|
||||
size_t offset = 0;
|
||||
// TODO: Use variation selector map for mRanges construction.
|
||||
// A font can have a glyph for a base code point and variation selector pair but no glyph for
|
||||
// the base code point without variation selector. The family won't be listed in the range in
|
||||
@@ -131,12 +132,11 @@ void FontCollection::init(const vector<std::shared_ptr<FontFamily>>& typefaces)
|
||||
#ifdef VERBOSE_DEBUG
|
||||
ALOGD("i=%zd: range start = %zd\n", i, offset);
|
||||
#endif
|
||||
range->start = offset;
|
||||
range->start = mFamilyVec.size();
|
||||
for (size_t j = 0; j < nTypefaces; j++) {
|
||||
if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
|
||||
const std::shared_ptr<FontFamily>& family = mFamilies[j];
|
||||
mFamilyVec.push_back(family);
|
||||
offset++;
|
||||
mFamilyVec.push_back(static_cast<uint8_t>(j));
|
||||
uint32_t nextChar = family->getCoverage().nextSetBit((i + 1) << kLogCharsPerPage);
|
||||
#ifdef VERBOSE_DEBUG
|
||||
ALOGD("nextChar = %d (j = %zd)\n", nextChar, j);
|
||||
@@ -144,8 +144,11 @@ void FontCollection::init(const vector<std::shared_ptr<FontFamily>>& typefaces)
|
||||
lastChar[j] = nextChar;
|
||||
}
|
||||
}
|
||||
range->end = offset;
|
||||
range->end = mFamilyVec.size();
|
||||
}
|
||||
// See the comment in Range for more details.
|
||||
LOG_ALWAYS_FATAL_IF(mFamilyVec.size() >= 0xFFFF,
|
||||
"Exceeded the maximum indexable cmap coverage.");
|
||||
}
|
||||
|
||||
// Special scores for the font fallback.
|
||||
@@ -286,11 +289,10 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(uint32_t ch,
|
||||
return mFamilies[0];
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<FontFamily>>& familyVec = (vs == 0) ? mFamilyVec : mFamilies;
|
||||
Range range = mRanges[ch >> kLogCharsPerPage];
|
||||
|
||||
if (vs != 0) {
|
||||
range = { 0, mFamilies.size() };
|
||||
range = { 0, static_cast<uint16_t>(mFamilies.size()) };
|
||||
}
|
||||
|
||||
#ifdef VERBOSE_DEBUG
|
||||
@@ -299,7 +301,8 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(uint32_t ch,
|
||||
int bestFamilyIndex = -1;
|
||||
uint32_t bestScore = kUnsupportedFontScore;
|
||||
for (size_t i = range.start; i < range.end; i++) {
|
||||
const std::shared_ptr<FontFamily>& family = familyVec[i];
|
||||
const std::shared_ptr<FontFamily>& family =
|
||||
vs == 0 ? mFamilies[mFamilyVec[i]] : mFamilies[i];
|
||||
const uint32_t score = calcFamilyScore(ch, vs, variant, langListId, family);
|
||||
if (score == kFirstFontScore) {
|
||||
// If the first font family supports the given character or variation sequence, always
|
||||
@@ -325,7 +328,7 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(uint32_t ch,
|
||||
}
|
||||
return mFamilies[0];
|
||||
}
|
||||
return familyVec[bestFamilyIndex];
|
||||
return vs == 0 ? mFamilies[mFamilyVec[bestFamilyIndex]] : mFamilies[bestFamilyIndex];
|
||||
}
|
||||
|
||||
const uint32_t NBSP = 0x00A0;
|
||||
|
||||
Reference in New Issue
Block a user