Skip to content

Commit 9b1204b

Browse files
author
Romain Guy
committed
Small code cleanup in FontRenderer
Change-Id: I09c00debe9b0b4f45b232cae402ed19bdaeabfe4
1 parent 9f5dab3 commit 9b1204b

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

libs/hwui/FontRenderer.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ FontRenderer::FontRenderer() {
4646
mMaxNumberOfQuads = 1024;
4747
mCurrentQuadIndex = 0;
4848

49-
mTextMeshPtr = NULL;
49+
mTextMesh = NULL;
5050
mCurrentCacheTexture = NULL;
5151
mLastCacheTexture = NULL;
5252

@@ -104,7 +104,7 @@ FontRenderer::~FontRenderer() {
104104
Caches::getInstance().unbindIndicesBuffer();
105105
glDeleteBuffers(1, &mIndexBufferID);
106106

107-
delete[] mTextMeshPtr;
107+
delete[] mTextMesh;
108108
}
109109

110110
Vector<Font*> fontsToDereference = mActiveFonts;
@@ -337,7 +337,7 @@ void FontRenderer::initVertexArrayBuffers() {
337337
uint32_t uvSize = 2;
338338
uint32_t vertsPerQuad = 4;
339339
uint32_t vertexBufferSize = mMaxNumberOfQuads * vertsPerQuad * coordSize * uvSize;
340-
mTextMeshPtr = new float[vertexBufferSize];
340+
mTextMesh = new float[vertexBufferSize];
341341
}
342342

343343
// We don't want to allocate anything unless we actually draw text
@@ -403,7 +403,7 @@ void FontRenderer::issueDrawCommand() {
403403
Caches& caches = Caches::getInstance();
404404
caches.bindIndicesBuffer(mIndexBufferID);
405405
if (!mDrawn) {
406-
float* buffer = mTextMeshPtr;
406+
float* buffer = mTextMesh;
407407
int offset = 2;
408408

409409
bool force = caches.unbindMeshBuffer();
@@ -432,7 +432,7 @@ void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
432432

433433
const uint32_t vertsPerQuad = 4;
434434
const uint32_t floatsPerVert = 4;
435-
float* currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert;
435+
float* currentPos = mTextMesh + mCurrentQuadIndex * vertsPerQuad * floatsPerVert;
436436

437437
(*currentPos++) = x1;
438438
(*currentPos++) = y1;
@@ -645,6 +645,19 @@ bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char
645645
return mDrawn;
646646
}
647647

648+
void FontRenderer::removeFont(const Font* font) {
649+
for (uint32_t ct = 0; ct < mActiveFonts.size(); ct++) {
650+
if (mActiveFonts[ct] == font) {
651+
mActiveFonts.removeAt(ct);
652+
break;
653+
}
654+
}
655+
656+
if (mCurrentFont == font) {
657+
mCurrentFont = NULL;
658+
}
659+
}
660+
648661
void FontRenderer::computeGaussianWeights(float* weights, int32_t radius) {
649662
// Compute gaussian weights for the blur
650663
// e is the euler's number
@@ -732,7 +745,6 @@ void FontRenderer::verticalBlur(float* weights, int32_t radius,
732745
float currentPixel = 0.0f;
733746

734747
for (int32_t y = 0; y < height; y ++) {
735-
736748
uint8_t* output = dest + y * width;
737749

738750
for (int32_t x = 0; x < width; x ++) {
@@ -766,7 +778,7 @@ void FontRenderer::verticalBlur(float* weights, int32_t radius,
766778
}
767779
}
768780
*output = (uint8_t) blurredPixel;
769-
output ++;
781+
output++;
770782
}
771783
}
772784
}

libs/hwui/FontRenderer.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class FontRenderer {
109109
return size;
110110
}
111111

112-
protected:
112+
private:
113113
friend class Font;
114114

115115
const uint8_t* mGammaTable;
@@ -143,6 +143,14 @@ class FontRenderer {
143143
float x3, float y3, float u3, float v3,
144144
float x4, float y4, float u4, float v4, CacheTexture* texture);
145145

146+
void removeFont(const Font* font);
147+
148+
void checkTextureUpdate();
149+
150+
void setTextureDirty() {
151+
mUploadTexture = true;
152+
}
153+
146154
uint32_t mSmallCacheWidth;
147155
uint32_t mSmallCacheHeight;
148156
uint32_t mLargeCacheWidth;
@@ -156,11 +164,10 @@ class FontRenderer {
156164
CacheTexture* mCurrentCacheTexture;
157165
CacheTexture* mLastCacheTexture;
158166

159-
void checkTextureUpdate();
160167
bool mUploadTexture;
161168

162169
// Pointer to vertex data to speed up frame to frame work
163-
float *mTextMeshPtr;
170+
float* mTextMesh;
164171
uint32_t mCurrentQuadIndex;
165172
uint32_t mMaxNumberOfQuads;
166173

@@ -174,12 +181,13 @@ class FontRenderer {
174181

175182
bool mLinearFiltering;
176183

177-
void computeGaussianWeights(float* weights, int32_t radius);
178-
void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
184+
/** We should consider multi-threading this code or using Renderscript **/
185+
static void computeGaussianWeights(float* weights, int32_t radius);
186+
static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
179187
int32_t width, int32_t height);
180-
void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
188+
static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
181189
int32_t width, int32_t height);
182-
void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
190+
static void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius);
183191
};
184192

185193
}; // namespace uirenderer

libs/hwui/font/CacheTexture.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,36 @@ CacheBlock* CacheBlock::insertBlock(CacheBlock* head, CacheBlock *newBlock) {
3737
newBlock, newBlock->mX, newBlock->mY,
3838
newBlock->mWidth, newBlock->mHeight);
3939
#endif
40+
4041
CacheBlock *currBlock = head;
4142
CacheBlock *prevBlock = NULL;
43+
4244
while (currBlock && currBlock->mY != TEXTURE_BORDER_SIZE) {
4345
if (newBlock->mWidth < currBlock->mWidth) {
4446
newBlock->mNext = currBlock;
4547
newBlock->mPrev = prevBlock;
4648
currBlock->mPrev = newBlock;
49+
4750
if (prevBlock) {
4851
prevBlock->mNext = newBlock;
4952
return head;
5053
} else {
5154
return newBlock;
5255
}
5356
}
57+
5458
prevBlock = currBlock;
5559
currBlock = currBlock->mNext;
5660
}
61+
5762
// new block larger than all others - insert at end (but before the remainder space, if there)
5863
newBlock->mNext = currBlock;
5964
newBlock->mPrev = prevBlock;
65+
6066
if (currBlock) {
6167
currBlock->mPrev = newBlock;
6268
}
69+
6370
if (prevBlock) {
6471
prevBlock->mNext = newBlock;
6572
return head;
@@ -74,18 +81,23 @@ CacheBlock* CacheBlock::removeBlock(CacheBlock* head, CacheBlock *blockToRemove)
7481
blockToRemove, blockToRemove->mX, blockToRemove->mY,
7582
blockToRemove->mWidth, blockToRemove->mHeight);
7683
#endif
84+
7785
CacheBlock* newHead = head;
7886
CacheBlock* nextBlock = blockToRemove->mNext;
7987
CacheBlock* prevBlock = blockToRemove->mPrev;
88+
8089
if (prevBlock) {
8190
prevBlock->mNext = nextBlock;
8291
} else {
8392
newHead = nextBlock;
8493
}
94+
8595
if (nextBlock) {
8696
nextBlock->mPrev = prevBlock;
8797
}
98+
8899
delete blockToRemove;
100+
89101
return newHead;
90102
}
91103

@@ -100,12 +112,14 @@ bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_
100112

101113
uint16_t glyphW = glyph.fWidth + TEXTURE_BORDER_SIZE;
102114
uint16_t glyphH = glyph.fHeight + TEXTURE_BORDER_SIZE;
115+
103116
// roundedUpW equals glyphW to the next multiple of CACHE_BLOCK_ROUNDING_SIZE.
104117
// This columns for glyphs that are close but not necessarily exactly the same size. It trades
105118
// off the loss of a few pixels for some glyphs against the ability to store more glyphs
106119
// of varying sizes in one block.
107120
uint16_t roundedUpW =
108121
(glyphW + CACHE_BLOCK_ROUNDING_SIZE - 1) & -CACHE_BLOCK_ROUNDING_SIZE;
122+
109123
CacheBlock *cacheBlock = mCacheBlocks;
110124
while (cacheBlock) {
111125
// Store glyph in this block iff: it fits the block's remaining space and:
@@ -118,15 +132,18 @@ bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_
118132
// Only enough space for this glyph - don't bother rounding up the width
119133
roundedUpW = glyphW;
120134
}
135+
121136
*retOriginX = cacheBlock->mX;
122137
*retOriginY = cacheBlock->mY;
138+
123139
// If this is the remainder space, create a new cache block for this column. Otherwise,
124140
// adjust the info about this column.
125141
if (cacheBlock->mY == TEXTURE_BORDER_SIZE) {
126142
uint16_t oldX = cacheBlock->mX;
127143
// Adjust remainder space dimensions
128144
cacheBlock->mWidth -= roundedUpW;
129145
cacheBlock->mX += roundedUpW;
146+
130147
if (mHeight - glyphH >= glyphH) {
131148
// There's enough height left over to create a new CacheBlock
132149
CacheBlock *newBlock = new CacheBlock(oldX, glyphH + TEXTURE_BORDER_SIZE,
@@ -148,16 +165,20 @@ bool CacheTexture::fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_
148165
cacheBlock->mWidth, cacheBlock->mHeight);
149166
#endif
150167
}
168+
151169
if (cacheBlock->mHeight < fmin(glyphH, glyphW)) {
152170
// If remaining space in this block is too small to be useful, remove it
153171
mCacheBlocks = CacheBlock::removeBlock(mCacheBlocks, cacheBlock);
154172
}
173+
155174
mDirty = true;
175+
mNumGlyphs++;
176+
156177
#if DEBUG_FONT_RENDERER
157178
ALOGD("fitBitmap: current block list:");
158179
mCacheBlocks->output();
159180
#endif
160-
++mNumGlyphs;
181+
161182
return true;
162183
}
163184
cacheBlock = cacheBlock->mNext;

libs/hwui/font/CacheTexture.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ struct CacheBlock {
5050
CacheBlock* mPrev;
5151

5252
CacheBlock(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool empty = false):
53-
mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL)
54-
{
53+
mX(x), mY(y), mWidth(width), mHeight(height), mNext(NULL), mPrev(NULL) {
5554
}
5655

5756
static CacheBlock* insertBlock(CacheBlock* head, CacheBlock *newBlock);

libs/hwui/font/Font.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ Font::Font(FontRenderer* state, uint32_t fontId, float fontSize,
4141

4242

4343
Font::~Font() {
44-
for (uint32_t ct = 0; ct < mState->mActiveFonts.size(); ct++) {
45-
if (mState->mActiveFonts[ct] == this) {
46-
mState->mActiveFonts.removeAt(ct);
47-
break;
48-
}
49-
}
44+
mState->removeFont(this);
5045

5146
for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
5247
delete mCachedGlyphs.valueAt(i);
@@ -405,7 +400,7 @@ void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyp
405400
glyph->mBitmapMaxU = endX / (float) cacheWidth;
406401
glyph->mBitmapMaxV = endY / (float) cacheHeight;
407402

408-
mState->mUploadTexture = true;
403+
mState->setTextureDirty();
409404
}
410405

411406
CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {

0 commit comments

Comments
 (0)