Skip to content

Commit 9d9758a

Browse files
author
Romain Guy
committed
Fix two memory leaks
Bug #6408362 Change-Id: I58543938e7b64d83504e11e97b0dd21ef8ebf3b6
1 parent ae91c4c commit 9d9758a

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

libs/hwui/FontRenderer.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,11 @@ void FontRenderer::flushAllAndInvalidate() {
540540
issueDrawCommand();
541541
mCurrentQuadIndex = 0;
542542
}
543+
543544
for (uint32_t i = 0; i < mActiveFonts.size(); i++) {
544545
mActiveFonts[i]->invalidateTextureCache();
545546
}
547+
546548
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
547549
mCacheLines[i]->mCurrentCol = 0;
548550
}
@@ -551,7 +553,7 @@ void FontRenderer::flushAllAndInvalidate() {
551553
void FontRenderer::deallocateTextureMemory(CacheTexture *cacheTexture) {
552554
if (cacheTexture && cacheTexture->mTexture) {
553555
glDeleteTextures(1, &cacheTexture->mTextureId);
554-
delete cacheTexture->mTexture;
556+
delete[] cacheTexture->mTexture;
555557
cacheTexture->mTexture = NULL;
556558
}
557559
}
@@ -582,11 +584,13 @@ void FontRenderer::flushLargeCaches() {
582584
deallocateTextureMemory(mCacheTexture512);
583585
}
584586

585-
void FontRenderer::allocateTextureMemory(CacheTexture *cacheTexture) {
587+
void FontRenderer::allocateTextureMemory(CacheTexture* cacheTexture) {
586588
int width = cacheTexture->mWidth;
587589
int height = cacheTexture->mHeight;
590+
588591
cacheTexture->mTexture = new uint8_t[width * height];
589592
memset(cacheTexture->mTexture, 0, width * height * sizeof(uint8_t));
593+
590594
glBindTexture(GL_TEXTURE_2D, cacheTexture->mTextureId);
591595
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
592596
// Initialize texture dimensions
@@ -654,11 +658,12 @@ void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyp
654658

655659
uint32_t cacheWidth = cacheLine->mMaxWidth;
656660

657-
CacheTexture *cacheTexture = cacheLine->mCacheTexture;
658-
if (cacheTexture->mTexture == NULL) {
661+
CacheTexture* cacheTexture = cacheLine->mCacheTexture;
662+
if (!cacheTexture->mTexture) {
659663
// Large-glyph texture memory is allocated only as needed
660664
allocateTextureMemory(cacheTexture);
661665
}
666+
662667
uint8_t* cacheBuffer = cacheTexture->mTexture;
663668
uint8_t* bitmapBuffer = (uint8_t*) glyph.fImage;
664669
unsigned int stride = glyph.rowBytes();
@@ -677,32 +682,40 @@ void FontRenderer::cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyp
677682
CacheTexture* FontRenderer::createCacheTexture(int width, int height, bool allocate) {
678683
GLuint textureId;
679684
glGenTextures(1, &textureId);
680-
uint8_t* textureMemory = NULL;
681685

686+
uint8_t* textureMemory = NULL;
682687
CacheTexture* cacheTexture = new CacheTexture(textureMemory, textureId, width, height);
688+
683689
if (allocate) {
684690
allocateTextureMemory(cacheTexture);
685691
}
692+
686693
return cacheTexture;
687694
}
688695

689696
void FontRenderer::initTextTexture() {
697+
for (uint32_t i = 0; i < mCacheLines.size(); i++) {
698+
delete mCacheLines[i];
699+
}
690700
mCacheLines.clear();
691701

702+
if (mCacheTextureSmall) {
703+
delete mCacheTextureSmall;
704+
delete mCacheTexture128;
705+
delete mCacheTexture256;
706+
delete mCacheTexture512;
707+
}
708+
692709
// Next, use other, separate caches for large glyphs.
693710
uint16_t maxWidth = 0;
694711
if (Caches::hasInstance()) {
695712
maxWidth = Caches::getInstance().maxTextureSize;
696713
}
714+
697715
if (maxWidth > MAX_TEXT_CACHE_WIDTH || maxWidth == 0) {
698716
maxWidth = MAX_TEXT_CACHE_WIDTH;
699717
}
700-
if (mCacheTextureSmall != NULL) {
701-
delete mCacheTextureSmall;
702-
delete mCacheTexture128;
703-
delete mCacheTexture256;
704-
delete mCacheTexture512;
705-
}
718+
706719
mCacheTextureSmall = createCacheTexture(mSmallCacheWidth, mSmallCacheHeight, true);
707720
mCacheTexture128 = createCacheTexture(maxWidth, 256, false);
708721
mCacheTexture256 = createCacheTexture(maxWidth, 256, false);

libs/hwui/FontRenderer.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ class FontRenderer;
6161

6262
class CacheTexture {
6363
public:
64-
CacheTexture(){}
64+
CacheTexture() { }
6565
CacheTexture(uint8_t* texture, GLuint textureId, uint16_t width, uint16_t height) :
66-
mTexture(texture), mTextureId(textureId), mWidth(width), mHeight(height),
67-
mLinearFiltering(false) {}
66+
mTexture(texture), mTextureId(textureId), mWidth(width), mHeight(height),
67+
mLinearFiltering(false) { }
6868
~CacheTexture() {
69-
if (mTexture != NULL) {
69+
if (mTexture) {
7070
delete[] mTexture;
7171
}
72-
if (mTextureId != 0) {
72+
if (mTextureId) {
7373
glDeleteTextures(1, &mTextureId);
7474
}
7575
}
@@ -90,7 +90,7 @@ class CacheTextureLine {
9090
mCurrentRow(currentRow),
9191
mCurrentCol(currentCol),
9292
mDirty(false),
93-
mCacheTexture(cacheTexture){
93+
mCacheTexture(cacheTexture) {
9494
}
9595

9696
bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);

0 commit comments

Comments
 (0)