Loading libs/rs/rsFont.cpp +55 −42 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,8 @@ using namespace android::renderscript; Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) { { mAllocFile = __FILE__; mAllocLine = __LINE__; mInitialized = false; mInitialized = false; mHasKerning = false; mHasKerning = false; } } Loading @@ -51,7 +53,7 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi) fullPath += fontsDir; fullPath += fontsDir; fullPath += name; fullPath += name; FT_Error error = FT_New_Face(mRSC->mStateFont.mLibrary, fullPath.string(), 0, &mFace); FT_Error error = FT_New_Face(mRSC->mStateFont.getLib(), fullPath.string(), 0, &mFace); if(error) { if(error) { LOGE("Unable to initialize font %s", fullPath.string()); LOGE("Unable to initialize font %s", fullPath.string()); return false; return false; Loading @@ -70,7 +72,6 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi) } } mHasKerning = FT_HAS_KERNING(mFace); mHasKerning = FT_HAS_KERNING(mFace); LOGE("Kerning: %i", mHasKerning); mInitialized = true; mInitialized = true; return true; return true; Loading Loading @@ -156,8 +157,6 @@ void Font::renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyp void Font::updateGlyphCache(CachedGlyphInfo *glyph) void Font::updateGlyphCache(CachedGlyphInfo *glyph) { { if(!glyph->mBitmapValid) { FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); if(error) { if(error) { LOGE("Couldn't load glyph."); LOGE("Couldn't load glyph."); Loading @@ -170,31 +169,25 @@ void Font::updateGlyphCache(CachedGlyphInfo *glyph) FT_Bitmap *bitmap = &mFace->glyph->bitmap; FT_Bitmap *bitmap = &mFace->glyph->bitmap; FT_Bitmap_New(&glyph->mBitmap); FT_Bitmap_Copy(mRSC->mStateFont.mLibrary, bitmap, &glyph->mBitmap); glyph->mBitmapValid = true; } // Now copy the bitmap into the cache texture // Now copy the bitmap into the cache texture uint32_t startX = 0; uint32_t startX = 0; uint32_t startY = 0; uint32_t startY = 0; // Let the font state figure out where to put the bitmap // Let the font state figure out where to put the bitmap FontState *state = &mRSC->mStateFont; FontState *state = &mRSC->mStateFont; glyph->mIsValid = state->cacheBitmap(&glyph->mBitmap, &startX, &startY); glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY); if(!glyph->mIsValid) { if(!glyph->mIsValid) { return; return; } } uint32_t endX = startX + glyph->mBitmap.width; uint32_t endX = startX + bitmap->width; uint32_t endY = startY + glyph->mBitmap.rows; uint32_t endY = startY + bitmap->rows; glyph->mBitmapMinX = startX; glyph->mBitmapMinX = startX; glyph->mBitmapMinY = startY; glyph->mBitmapMinY = startY; glyph->mBitmapWidth = glyph->mBitmap.width; glyph->mBitmapWidth = bitmap->width; glyph->mBitmapHeight = glyph->mBitmap.rows; glyph->mBitmapHeight = bitmap->rows; uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); Loading @@ -212,7 +205,6 @@ Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph) newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); newGlyph->mIsValid = false; newGlyph->mIsValid = false; newGlyph->mBitmapValid = false; //LOGE("Glyph = %c, face index: %u", (unsigned char)glyph, newGlyph->mGlyphIndex); //LOGE("Glyph = %c, face index: %u", (unsigned char)glyph, newGlyph->mGlyphIndex); Loading @@ -228,7 +220,6 @@ Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t for(uint32_t i = 0; i < activeFonts.size(); i ++) { for(uint32_t i = 0; i < activeFonts.size(); i ++) { Font *ithFont = activeFonts[i]; Font *ithFont = activeFonts[i]; if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { ithFont->incUserRef(); return ithFont; return ithFont; } } } } Loading @@ -236,7 +227,6 @@ Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t Font *newFont = new Font(rsc); Font *newFont = new Font(rsc); bool isInitialized = newFont->init(name, fontSize, dpi); bool isInitialized = newFont->init(name, fontSize, dpi); if(isInitialized) { if(isInitialized) { newFont->incUserRef(); activeFonts.push(newFont); activeFonts.push(newFont); return newFont; return newFont; } } Loading @@ -261,9 +251,6 @@ Font::~Font() for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); if(glyph->mBitmapValid) { FT_Bitmap_Done(mRSC->mStateFont.mLibrary, &glyph->mBitmap); } delete glyph; delete glyph; } } } } Loading @@ -285,21 +272,23 @@ FontState::~FontState() rsAssert(!mActiveFonts.size()); rsAssert(!mActiveFonts.size()); } } void FontState::init(Context *rsc) FT_Library FontState::getLib() { { FT_Error error; if(!mLibrary) { if(!mLibrary) { error = FT_Init_FreeType(&mLibrary); FT_Error error = FT_Init_FreeType(&mLibrary); if(error) { if(error) { LOGE("Unable to initialize freetype"); LOGE("Unable to initialize freetype"); return; return NULL; } } } return mLibrary; } } mRSC = rsc; void FontState::init(Context *rsc) { //getLib(); mDefault.set(Font::create(rsc, "DroidSans.ttf", 16, 96)); mRSC = rsc; } } void FontState::flushAllAndInvalidate() void FontState::flushAllAndInvalidate() Loading Loading @@ -627,10 +616,16 @@ void FontState::renderText(const char *text, uint32_t len, uint32_t startIndex, { { checkInit(); checkInit(); String8 text8(text); //String8 text8(text); // Render code here // Render code here Font *currentFont = mRSC->getFont(); Font *currentFont = mRSC->getFont(); if(!currentFont) { if(!mDefault.get()) { mDefault.set(Font::create(mRSC, "DroidSans.ttf", 16, 96)); } currentFont = mDefault.get(); } currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y); currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y); if(mCurrentQuadIndex != 0) { if(mCurrentQuadIndex != 0) { Loading Loading @@ -669,12 +664,26 @@ void FontState::renderText(Allocation *alloc, uint32_t start, int len, int x, in void FontState::deinit(Context *rsc) void FontState::deinit(Context *rsc) { { if(mLibrary) { mInitialized = false; FT_Done_FreeType( mLibrary ); mIndexBuffer.clear(); mVertexArray.clear(); mFontShaderF.clear(); mFontSampler.clear(); mFontProgramStore.clear(); mTextTexture.clear(); for(uint32_t i = 0; i < mCacheLines.size(); i ++) { delete mCacheLines[i]; } } mCacheLines.clear(); delete mDefault.get(); mDefault.clear(); mDefault.clear(); if(mLibrary) { FT_Done_FreeType( mLibrary ); } } } namespace android { namespace android { Loading @@ -682,7 +691,11 @@ namespace renderscript { RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi) RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi) { { return Font::create(rsc, name, fontSize, dpi); Font *newFont = Font::create(rsc, name, fontSize, dpi); if(newFont) { newFont->incUserRef(); } return newFont; } } } // renderscript } // renderscript Loading libs/rs/rsFont.h +1 −3 Original line number Original line Diff line number Diff line Loading @@ -79,9 +79,6 @@ protected: // Values below contain a glyph's origin in the bitmap // Values below contain a glyph's origin in the bitmap FT_Int mBitmapLeft; FT_Int mBitmapLeft; FT_Int mBitmapTop; FT_Int mBitmapTop; // Hold on to the bitmap in case cache is invalidated FT_Bitmap mBitmap; bool mBitmapValid; }; }; String8 mFontName; String8 mFontName; Loading Loading @@ -156,6 +153,7 @@ protected: // Free type library, we only need one copy // Free type library, we only need one copy FT_Library mLibrary; FT_Library mLibrary; FT_Library getLib(); Vector<Font*> mActiveFonts; Vector<Font*> mActiveFonts; // Render state for the font // Render state for the font Loading Loading
libs/rs/rsFont.cpp +55 −42 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,8 @@ using namespace android::renderscript; Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL) { { mAllocFile = __FILE__; mAllocLine = __LINE__; mInitialized = false; mInitialized = false; mHasKerning = false; mHasKerning = false; } } Loading @@ -51,7 +53,7 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi) fullPath += fontsDir; fullPath += fontsDir; fullPath += name; fullPath += name; FT_Error error = FT_New_Face(mRSC->mStateFont.mLibrary, fullPath.string(), 0, &mFace); FT_Error error = FT_New_Face(mRSC->mStateFont.getLib(), fullPath.string(), 0, &mFace); if(error) { if(error) { LOGE("Unable to initialize font %s", fullPath.string()); LOGE("Unable to initialize font %s", fullPath.string()); return false; return false; Loading @@ -70,7 +72,6 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi) } } mHasKerning = FT_HAS_KERNING(mFace); mHasKerning = FT_HAS_KERNING(mFace); LOGE("Kerning: %i", mHasKerning); mInitialized = true; mInitialized = true; return true; return true; Loading Loading @@ -156,8 +157,6 @@ void Font::renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyp void Font::updateGlyphCache(CachedGlyphInfo *glyph) void Font::updateGlyphCache(CachedGlyphInfo *glyph) { { if(!glyph->mBitmapValid) { FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER ); if(error) { if(error) { LOGE("Couldn't load glyph."); LOGE("Couldn't load glyph."); Loading @@ -170,31 +169,25 @@ void Font::updateGlyphCache(CachedGlyphInfo *glyph) FT_Bitmap *bitmap = &mFace->glyph->bitmap; FT_Bitmap *bitmap = &mFace->glyph->bitmap; FT_Bitmap_New(&glyph->mBitmap); FT_Bitmap_Copy(mRSC->mStateFont.mLibrary, bitmap, &glyph->mBitmap); glyph->mBitmapValid = true; } // Now copy the bitmap into the cache texture // Now copy the bitmap into the cache texture uint32_t startX = 0; uint32_t startX = 0; uint32_t startY = 0; uint32_t startY = 0; // Let the font state figure out where to put the bitmap // Let the font state figure out where to put the bitmap FontState *state = &mRSC->mStateFont; FontState *state = &mRSC->mStateFont; glyph->mIsValid = state->cacheBitmap(&glyph->mBitmap, &startX, &startY); glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY); if(!glyph->mIsValid) { if(!glyph->mIsValid) { return; return; } } uint32_t endX = startX + glyph->mBitmap.width; uint32_t endX = startX + bitmap->width; uint32_t endY = startY + glyph->mBitmap.rows; uint32_t endY = startY + bitmap->rows; glyph->mBitmapMinX = startX; glyph->mBitmapMinX = startX; glyph->mBitmapMinY = startY; glyph->mBitmapMinY = startY; glyph->mBitmapWidth = glyph->mBitmap.width; glyph->mBitmapWidth = bitmap->width; glyph->mBitmapHeight = glyph->mBitmap.rows; glyph->mBitmapHeight = bitmap->rows; uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); uint32_t cacheWidth = state->getCacheTextureType()->getDimX(); uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); uint32_t cacheHeight = state->getCacheTextureType()->getDimY(); Loading @@ -212,7 +205,6 @@ Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph) newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph); newGlyph->mIsValid = false; newGlyph->mIsValid = false; newGlyph->mBitmapValid = false; //LOGE("Glyph = %c, face index: %u", (unsigned char)glyph, newGlyph->mGlyphIndex); //LOGE("Glyph = %c, face index: %u", (unsigned char)glyph, newGlyph->mGlyphIndex); Loading @@ -228,7 +220,6 @@ Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t for(uint32_t i = 0; i < activeFonts.size(); i ++) { for(uint32_t i = 0; i < activeFonts.size(); i ++) { Font *ithFont = activeFonts[i]; Font *ithFont = activeFonts[i]; if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) { ithFont->incUserRef(); return ithFont; return ithFont; } } } } Loading @@ -236,7 +227,6 @@ Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t Font *newFont = new Font(rsc); Font *newFont = new Font(rsc); bool isInitialized = newFont->init(name, fontSize, dpi); bool isInitialized = newFont->init(name, fontSize, dpi); if(isInitialized) { if(isInitialized) { newFont->incUserRef(); activeFonts.push(newFont); activeFonts.push(newFont); return newFont; return newFont; } } Loading @@ -261,9 +251,6 @@ Font::~Font() for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) { CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i); if(glyph->mBitmapValid) { FT_Bitmap_Done(mRSC->mStateFont.mLibrary, &glyph->mBitmap); } delete glyph; delete glyph; } } } } Loading @@ -285,21 +272,23 @@ FontState::~FontState() rsAssert(!mActiveFonts.size()); rsAssert(!mActiveFonts.size()); } } void FontState::init(Context *rsc) FT_Library FontState::getLib() { { FT_Error error; if(!mLibrary) { if(!mLibrary) { error = FT_Init_FreeType(&mLibrary); FT_Error error = FT_Init_FreeType(&mLibrary); if(error) { if(error) { LOGE("Unable to initialize freetype"); LOGE("Unable to initialize freetype"); return; return NULL; } } } return mLibrary; } } mRSC = rsc; void FontState::init(Context *rsc) { //getLib(); mDefault.set(Font::create(rsc, "DroidSans.ttf", 16, 96)); mRSC = rsc; } } void FontState::flushAllAndInvalidate() void FontState::flushAllAndInvalidate() Loading Loading @@ -627,10 +616,16 @@ void FontState::renderText(const char *text, uint32_t len, uint32_t startIndex, { { checkInit(); checkInit(); String8 text8(text); //String8 text8(text); // Render code here // Render code here Font *currentFont = mRSC->getFont(); Font *currentFont = mRSC->getFont(); if(!currentFont) { if(!mDefault.get()) { mDefault.set(Font::create(mRSC, "DroidSans.ttf", 16, 96)); } currentFont = mDefault.get(); } currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y); currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y); if(mCurrentQuadIndex != 0) { if(mCurrentQuadIndex != 0) { Loading Loading @@ -669,12 +664,26 @@ void FontState::renderText(Allocation *alloc, uint32_t start, int len, int x, in void FontState::deinit(Context *rsc) void FontState::deinit(Context *rsc) { { if(mLibrary) { mInitialized = false; FT_Done_FreeType( mLibrary ); mIndexBuffer.clear(); mVertexArray.clear(); mFontShaderF.clear(); mFontSampler.clear(); mFontProgramStore.clear(); mTextTexture.clear(); for(uint32_t i = 0; i < mCacheLines.size(); i ++) { delete mCacheLines[i]; } } mCacheLines.clear(); delete mDefault.get(); mDefault.clear(); mDefault.clear(); if(mLibrary) { FT_Done_FreeType( mLibrary ); } } } namespace android { namespace android { Loading @@ -682,7 +691,11 @@ namespace renderscript { RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi) RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi) { { return Font::create(rsc, name, fontSize, dpi); Font *newFont = Font::create(rsc, name, fontSize, dpi); if(newFont) { newFont->incUserRef(); } return newFont; } } } // renderscript } // renderscript Loading
libs/rs/rsFont.h +1 −3 Original line number Original line Diff line number Diff line Loading @@ -79,9 +79,6 @@ protected: // Values below contain a glyph's origin in the bitmap // Values below contain a glyph's origin in the bitmap FT_Int mBitmapLeft; FT_Int mBitmapLeft; FT_Int mBitmapTop; FT_Int mBitmapTop; // Hold on to the bitmap in case cache is invalidated FT_Bitmap mBitmap; bool mBitmapValid; }; }; String8 mFontName; String8 mFontName; Loading Loading @@ -156,6 +153,7 @@ protected: // Free type library, we only need one copy // Free type library, we only need one copy FT_Library mLibrary; FT_Library mLibrary; FT_Library getLib(); Vector<Font*> mActiveFonts; Vector<Font*> mActiveFonts; // Render state for the font // Render state for the font Loading