Skip to content

Commit c83f982

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "Code cleanup in FontRenderer" into jb-mr1-dev
2 parents 7950d08 + 8087246 commit c83f982

File tree

5 files changed

+122
-82
lines changed

5 files changed

+122
-82
lines changed

libs/hwui/FontRenderer.cpp

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -127,68 +127,34 @@ void FontRenderer::flushAllAndInvalidate() {
127127
mCacheTextures[i]->init();
128128
}
129129

130-
#if DEBUG_FONT_RENDERER
130+
#if DEBUG_FONT_RENDERER
131131
uint16_t totalGlyphs = 0;
132132
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
133-
totalGlyphs += mCacheTextures[i]->mNumGlyphs;
133+
totalGlyphs += mCacheTextures[i]->getGlyphCount();
134134
// Erase caches, just as a debugging facility
135-
if (mCacheTextures[i]->mTexture) {
136-
memset(mCacheTextures[i]->mTexture, 0,
137-
mCacheTextures[i]->mWidth * mCacheTextures[i]->mHeight);
135+
if (mCacheTextures[i]->getTexture()) {
136+
memset(mCacheTextures[i]->getTexture(), 0,
137+
mCacheTextures[i]->getWidth() * mCacheTextures[i]->getHeight());
138138
}
139139
}
140140
ALOGD("Flushing caches: glyphs cached = %d", totalGlyphs);
141141
#endif
142142
}
143143

144-
void FontRenderer::deallocateTextureMemory(CacheTexture *cacheTexture) {
145-
if (cacheTexture && cacheTexture->mTexture) {
146-
glDeleteTextures(1, &cacheTexture->mTextureId);
147-
delete[] cacheTexture->mTexture;
148-
cacheTexture->mTexture = NULL;
149-
cacheTexture->mTextureId = 0;
150-
}
151-
}
152-
153144
void FontRenderer::flushLargeCaches() {
154145
// Start from 1; don't deallocate smallest/default texture
155146
for (uint32_t i = 1; i < mCacheTextures.size(); i++) {
156147
CacheTexture* cacheTexture = mCacheTextures[i];
157-
if (cacheTexture->mTexture != NULL) {
148+
if (cacheTexture->getTexture()) {
158149
cacheTexture->init();
159150
for (uint32_t j = 0; j < mActiveFonts.size(); j++) {
160151
mActiveFonts[j]->invalidateTextureCache(cacheTexture);
161152
}
162-
deallocateTextureMemory(cacheTexture);
153+
cacheTexture->releaseTexture();
163154
}
164155
}
165156
}
166157

167-
void FontRenderer::allocateTextureMemory(CacheTexture* cacheTexture) {
168-
int width = cacheTexture->mWidth;
169-
int height = cacheTexture->mHeight;
170-
171-
cacheTexture->mTexture = new uint8_t[width * height];
172-
173-
if (!cacheTexture->mTextureId) {
174-
glGenTextures(1, &cacheTexture->mTextureId);
175-
}
176-
177-
Caches::getInstance().activeTexture(0);
178-
glBindTexture(GL_TEXTURE_2D, cacheTexture->mTextureId);
179-
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
180-
// Initialize texture dimensions
181-
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
182-
GL_ALPHA, GL_UNSIGNED_BYTE, 0);
183-
184-
const GLenum filtering = cacheTexture->mLinearFiltering ? GL_LINEAR : GL_NEAREST;
185-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
186-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
187-
188-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
189-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
190-
}
191-
192158
CacheTexture* FontRenderer::cacheBitmapInTexture(const SkGlyph& glyph,
193159
uint32_t* startX, uint32_t* startY) {
194160
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
@@ -206,7 +172,7 @@ void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyp
206172
cachedGlyph->mIsValid = false;
207173
// If the glyph is too tall, don't cache it
208174
if (glyph.fHeight + TEXTURE_BORDER_SIZE * 2 >
209-
mCacheTextures[mCacheTextures.size() - 1]->mHeight) {
175+
mCacheTextures[mCacheTextures.size() - 1]->getHeight()) {
210176
ALOGE("Font size too large to fit in cache. width, height = %i, %i",
211177
(int) glyph.fWidth, (int) glyph.fHeight);
212178
return;
@@ -240,14 +206,15 @@ void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyp
240206
uint32_t endX = startX + glyph.fWidth;
241207
uint32_t endY = startY + glyph.fHeight;
242208

243-
uint32_t cacheWidth = cacheTexture->mWidth;
209+
uint32_t cacheWidth = cacheTexture->getWidth();
244210

245-
if (!cacheTexture->mTexture) {
211+
if (!cacheTexture->getTexture()) {
212+
Caches::getInstance().activeTexture(0);
246213
// Large-glyph texture memory is allocated only as needed
247-
allocateTextureMemory(cacheTexture);
214+
cacheTexture->allocateTexture();
248215
}
249216

250-
uint8_t* cacheBuffer = cacheTexture->mTexture;
217+
uint8_t* cacheBuffer = cacheTexture->getTexture();
251218
uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage;
252219
unsigned int stride = glyph.rowBytes();
253220

@@ -287,7 +254,8 @@ CacheTexture* FontRenderer::createCacheTexture(int width, int height, bool alloc
287254
CacheTexture* cacheTexture = new CacheTexture(width, height);
288255

289256
if (allocate) {
290-
allocateTextureMemory(cacheTexture);
257+
Caches::getInstance().activeTexture(0);
258+
cacheTexture->allocateTexture();
291259
}
292260

293261
return cacheTexture;
@@ -362,16 +330,16 @@ void FontRenderer::checkTextureUpdate() {
362330
// Iterate over all the cache textures and see which ones need to be updated
363331
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
364332
CacheTexture* cacheTexture = mCacheTextures[i];
365-
if (cacheTexture->mDirty && cacheTexture->mTexture != NULL) {
333+
if (cacheTexture->isDirty() && cacheTexture->getTexture()) {
366334
uint32_t xOffset = 0;
367-
uint32_t width = cacheTexture->mWidth;
368-
uint32_t height = cacheTexture->mHeight;
369-
void* textureData = cacheTexture->mTexture;
335+
uint32_t width = cacheTexture->getWidth();
336+
uint32_t height = cacheTexture->getHeight();
337+
void* textureData = cacheTexture->getTexture();
370338

371-
if (cacheTexture->mTextureId != lastTextureId) {
339+
if (cacheTexture->getTextureId() != lastTextureId) {
340+
lastTextureId = cacheTexture->getTextureId();
372341
caches.activeTexture(0);
373-
glBindTexture(GL_TEXTURE_2D, cacheTexture->mTextureId);
374-
lastTextureId = cacheTexture->mTextureId;
342+
glBindTexture(GL_TEXTURE_2D, lastTextureId);
375343
}
376344
#if DEBUG_FONT_RENDERER
377345
ALOGD("glTextSubimage for cacheTexture %d: xOff, width height = %d, %d, %d",
@@ -380,18 +348,14 @@ void FontRenderer::checkTextureUpdate() {
380348
glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, 0, width, height,
381349
GL_ALPHA, GL_UNSIGNED_BYTE, textureData);
382350

383-
cacheTexture->mDirty = false;
351+
cacheTexture->setDirty(false);
384352
}
385353
}
386354

387355
caches.activeTexture(0);
388-
glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->mTextureId);
389-
if (mLinearFiltering != mCurrentCacheTexture->mLinearFiltering) {
390-
const GLenum filtering = mLinearFiltering ? GL_LINEAR : GL_NEAREST;
391-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
392-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
393-
mCurrentCacheTexture->mLinearFiltering = mLinearFiltering;
394-
}
356+
glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->getTextureId());
357+
358+
mCurrentCacheTexture->setLinearFiltering(mLinearFiltering, false);
395359
mLastCacheTexture = mCurrentCacheTexture;
396360

397361
mUploadTexture = false;

libs/hwui/FontRenderer.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,18 @@ class FontRenderer {
8585
GLuint getTexture(bool linearFiltering = false) {
8686
checkInit();
8787

88-
if (linearFiltering != mCurrentCacheTexture->mLinearFiltering) {
89-
mCurrentCacheTexture->mLinearFiltering = linearFiltering;
90-
mLinearFiltering = linearFiltering;
91-
const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST;
92-
93-
glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->mTextureId);
94-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
95-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
96-
}
88+
mCurrentCacheTexture->setLinearFiltering(linearFiltering);
89+
mLinearFiltering = linearFiltering;
9790

98-
return mCurrentCacheTexture->mTextureId;
91+
return mCurrentCacheTexture->getTextureId();
9992
}
10093

10194
uint32_t getCacheSize() const {
10295
uint32_t size = 0;
10396
for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
10497
CacheTexture* cacheTexture = mCacheTextures[i];
105-
if (cacheTexture != NULL && cacheTexture->mTexture != NULL) {
106-
size += cacheTexture->mWidth * cacheTexture->mHeight;
98+
if (cacheTexture && cacheTexture->getTexture()) {
99+
size += cacheTexture->getWidth() * cacheTexture->getHeight();
107100
}
108101
}
109102
return size;

libs/hwui/font/CacheTexture.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,91 @@ class CacheTexture {
103103
mWidth - TEXTURE_BORDER_SIZE, mHeight - TEXTURE_BORDER_SIZE, true);
104104
}
105105

106+
void releaseTexture() {
107+
if (mTexture) {
108+
glDeleteTextures(1, &mTextureId);
109+
delete[] mTexture;
110+
mTexture = NULL;
111+
mTextureId = 0;
112+
}
113+
}
114+
115+
/**
116+
* This method assumes that the proper texture unit is active.
117+
*/
118+
void allocateTexture() {
119+
int width = mWidth;
120+
int height = mHeight;
121+
122+
mTexture = new uint8_t[width * height];
123+
124+
if (!mTextureId) {
125+
glGenTextures(1, &mTextureId);
126+
}
127+
128+
glBindTexture(GL_TEXTURE_2D, mTextureId);
129+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130+
// Initialize texture dimensions
131+
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
132+
GL_ALPHA, GL_UNSIGNED_BYTE, 0);
133+
134+
const GLenum filtering = getLinearFiltering() ? GL_LINEAR : GL_NEAREST;
135+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
136+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
137+
138+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
139+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
140+
}
141+
106142
bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
107143

144+
inline uint16_t getWidth() const {
145+
return mWidth;
146+
}
147+
148+
inline uint16_t getHeight() const {
149+
return mHeight;
150+
}
151+
152+
inline uint8_t* getTexture() const {
153+
return mTexture;
154+
}
155+
156+
inline GLuint getTextureId() const {
157+
return mTextureId;
158+
}
159+
160+
inline bool isDirty() const {
161+
return mDirty;
162+
}
163+
164+
inline void setDirty(bool dirty) {
165+
mDirty = dirty;
166+
}
167+
168+
inline bool getLinearFiltering() const {
169+
return mLinearFiltering;
170+
}
171+
172+
/**
173+
* This method assumes that the proper texture unit is active.
174+
*/
175+
void setLinearFiltering(bool linearFiltering, bool bind = true) {
176+
if (linearFiltering != mLinearFiltering) {
177+
mLinearFiltering = linearFiltering;
178+
179+
const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST;
180+
if (bind) glBindTexture(GL_TEXTURE_2D, getTextureId());
181+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering);
182+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering);
183+
}
184+
}
185+
186+
inline uint16_t getGlyphCount() const {
187+
return mNumGlyphs;
188+
}
189+
190+
private:
108191
uint8_t* mTexture;
109192
GLuint mTextureId;
110193
uint16_t mWidth;

libs/hwui/font/Font.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ Font::~Font() {
4848
}
4949
}
5050

51-
void Font::invalidateTextureCache(CacheTexture *cacheTexture) {
51+
void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
5252
for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
5353
CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
54-
if (cacheTexture == NULL || cachedGlyph->mCacheTexture == cacheTexture) {
54+
if (cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
5555
cachedGlyph->mIsValid = false;
5656
}
5757
}
@@ -106,9 +106,9 @@ void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
106106
uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
107107
uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
108108

109-
CacheTexture *cacheTexture = glyph->mCacheTexture;
110-
uint32_t cacheWidth = cacheTexture->mWidth;
111-
const uint8_t* cacheBuffer = cacheTexture->mTexture;
109+
CacheTexture* cacheTexture = glyph->mCacheTexture;
110+
uint32_t cacheWidth = cacheTexture->getWidth();
111+
const uint8_t* cacheBuffer = cacheTexture->getTexture();
112112

113113
uint32_t cacheX = 0, cacheY = 0;
114114
int32_t bX = 0, bY = 0;
@@ -392,8 +392,8 @@ void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyp
392392
glyph->mBitmapWidth = skiaGlyph.fWidth;
393393
glyph->mBitmapHeight = skiaGlyph.fHeight;
394394

395-
uint32_t cacheWidth = glyph->mCacheTexture->mWidth;
396-
uint32_t cacheHeight = glyph->mCacheTexture->mHeight;
395+
uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
396+
uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
397397

398398
glyph->mBitmapMinU = startX / (float) cacheWidth;
399399
glyph->mBitmapMinV = startY / (float) cacheHeight;

libs/hwui/font/Font.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Font {
9494
// Cache of glyphs
9595
DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
9696

97-
void invalidateTextureCache(CacheTexture *cacheTexture = NULL);
97+
void invalidateTextureCache(CacheTexture* cacheTexture = NULL);
9898

9999
CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching);
100100
void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,

0 commit comments

Comments
 (0)