Purge hb font on Minikin font destruction

This patch eagerly purges the corresponding hb_font_t object from
the HbFontCache when the underlying MinikinFont is destroyed. After
that, the key will no longer be accessed, so having the entry is
wastes memory.

Bug: 27251075
Bug: 27860101
Change-Id: I1b98016133fe3baf6525ac37d970a65ddccadb4f
This commit is contained in:
Raph Levien
2016-04-07 12:20:12 -07:00
parent dbcbe1f426
commit 1ea4165cef
5 changed files with 40 additions and 0 deletions

View File

@@ -96,6 +96,8 @@ class MinikinFontFreeType;
class MinikinFont : public MinikinRefCounted { class MinikinFont : public MinikinRefCounted {
public: public:
virtual ~MinikinFont();
virtual float GetHorizontalAdvance(uint32_t glyph_id, virtual float GetHorizontalAdvance(uint32_t glyph_id,
const MinikinPaint &paint) const = 0; const MinikinPaint &paint) const = 0;

View File

@@ -32,6 +32,7 @@ minikin_src_files := \
Measurement.cpp \ Measurement.cpp \
MinikinInternal.cpp \ MinikinInternal.cpp \
MinikinRefCounted.cpp \ MinikinRefCounted.cpp \
MinikinFont.cpp \
MinikinFontFreeType.cpp \ MinikinFontFreeType.cpp \
SparseBitSet.cpp \ SparseBitSet.cpp \
WordBreaker.cpp WordBreaker.cpp

View File

@@ -75,6 +75,10 @@ public:
mCache.clear(); mCache.clear();
} }
void remove(int32_t fontId) {
mCache.remove(fontId);
}
private: private:
static const size_t kMaxEntries = 100; static const size_t kMaxEntries = 100;
@@ -95,6 +99,12 @@ void purgeHbFontCacheLocked() {
getFontCacheLocked()->clear(); getFontCacheLocked()->clear();
} }
void purgeHbFont(const MinikinFont* minikinFont) {
AutoMutex _l(gMinikinLock);
const int32_t fontId = minikinFont->GetUniqueId();
getFontCacheLocked()->remove(fontId);
}
hb_font_t* getHbFontLocked(MinikinFont* minikinFont) { hb_font_t* getHbFontLocked(MinikinFont* minikinFont) {
assertMinikinLocked(); assertMinikinLocked();
static hb_font_t* nullFaceFont = nullptr; static hb_font_t* nullFaceFont = nullptr;

View File

@@ -23,6 +23,7 @@ namespace android {
class MinikinFont; class MinikinFont;
void purgeHbFontCacheLocked(); void purgeHbFontCacheLocked();
void purgeHbFont(const MinikinFont* minikinFont);
hb_font_t* getHbFontLocked(MinikinFont* minikinFont); hb_font_t* getHbFontLocked(MinikinFont* minikinFont);
} // namespace android } // namespace android

View File

@@ -0,0 +1,26 @@
/*
* 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 <minikin/MinikinFont.h>
#include "HbFontCache.h"
namespace android {
MinikinFont::~MinikinFont() {
purgeHbFont(this);
}
} // namespace android