Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 071508d9 authored by Alex Sakhartchouk's avatar Alex Sakhartchouk
Browse files

Removing font initialization on startup

Change-Id: I6f28204c3d431955fbf0f2f74dde09012bba0a4d
parent e519b37f
Loading
Loading
Loading
Loading
+55 −42
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ using namespace android::renderscript;

Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL)
{
    mAllocFile = __FILE__;
    mAllocLine = __LINE__;
    mInitialized = false;
    mHasKerning = false;
}
@@ -51,7 +53,7 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi)
    fullPath += fontsDir;
    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) {
        LOGE("Unable to initialize font %s", fullPath.string());
        return false;
@@ -70,7 +72,6 @@ bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi)
    }

    mHasKerning = FT_HAS_KERNING(mFace);
    LOGE("Kerning: %i", mHasKerning);

    mInitialized = true;
    return true;
@@ -156,8 +157,6 @@ void Font::renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyp

void Font::updateGlyphCache(CachedGlyphInfo *glyph)
{
    if(!glyph->mBitmapValid) {

    FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER );
    if(error) {
        LOGE("Couldn't load glyph.");
@@ -170,31 +169,25 @@ void Font::updateGlyphCache(CachedGlyphInfo *glyph)

    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
    uint32_t startX = 0;
    uint32_t startY = 0;

    // Let the font state figure out where to put the bitmap
    FontState *state = &mRSC->mStateFont;
    glyph->mIsValid = state->cacheBitmap(&glyph->mBitmap, &startX, &startY);
    glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY);

    if(!glyph->mIsValid) {
        return;
    }

    uint32_t endX = startX + glyph->mBitmap.width;
    uint32_t endY = startY + glyph->mBitmap.rows;
    uint32_t endX = startX + bitmap->width;
    uint32_t endY = startY + bitmap->rows;

    glyph->mBitmapMinX = startX;
    glyph->mBitmapMinY = startY;
    glyph->mBitmapWidth = glyph->mBitmap.width;
    glyph->mBitmapHeight = glyph->mBitmap.rows;
    glyph->mBitmapWidth = bitmap->width;
    glyph->mBitmapHeight = bitmap->rows;

    uint32_t cacheWidth = state->getCacheTextureType()->getDimX();
    uint32_t cacheHeight = state->getCacheTextureType()->getDimY();
@@ -212,7 +205,6 @@ Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph)

    newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph);
    newGlyph->mIsValid = false;
    newGlyph->mBitmapValid = false;

    //LOGE("Glyph = %c, face index: %u", (unsigned char)glyph, newGlyph->mGlyphIndex);

@@ -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 ++) {
        Font *ithFont = activeFonts[i];
        if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) {
            ithFont->incUserRef();
            return ithFont;
        }
    }
@@ -236,7 +227,6 @@ Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t
    Font *newFont = new Font(rsc);
    bool isInitialized = newFont->init(name, fontSize, dpi);
    if(isInitialized) {
        newFont->incUserRef();
        activeFonts.push(newFont);
        return newFont;
    }
@@ -261,9 +251,6 @@ Font::~Font()

    for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) {
        CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i);
        if(glyph->mBitmapValid) {
            FT_Bitmap_Done(mRSC->mStateFont.mLibrary, &glyph->mBitmap);
        }
        delete glyph;
    }
}
@@ -285,21 +272,23 @@ FontState::~FontState()
    rsAssert(!mActiveFonts.size());
}

void FontState::init(Context *rsc)
FT_Library FontState::getLib()
{
    FT_Error error;

    if(!mLibrary) {
        error = FT_Init_FreeType(&mLibrary);
        FT_Error error = FT_Init_FreeType(&mLibrary);
        if(error) {
            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()
@@ -627,10 +616,16 @@ void FontState::renderText(const char *text, uint32_t len, uint32_t startIndex,
{
    checkInit();

    String8 text8(text);
    //String8 text8(text);

    // Render code here
    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);

    if(mCurrentQuadIndex != 0) {
@@ -669,12 +664,26 @@ void FontState::renderText(Allocation *alloc, uint32_t start, int len, int x, in

void FontState::deinit(Context *rsc)
{
    if(mLibrary) {
        FT_Done_FreeType( mLibrary );
    mInitialized = false;

    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();

    if(mLibrary) {
        FT_Done_FreeType( mLibrary );
    }
}

namespace android {
@@ -682,7 +691,11 @@ namespace renderscript {

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
+1 −3
Original line number Diff line number Diff line
@@ -79,9 +79,6 @@ protected:
        // Values below contain a glyph's origin in the bitmap
        FT_Int mBitmapLeft;
        FT_Int mBitmapTop;
        // Hold on to the bitmap in case cache is invalidated
        FT_Bitmap mBitmap;
        bool mBitmapValid;
    };

    String8 mFontName;
@@ -156,6 +153,7 @@ protected:

    // Free type library, we only need one copy
    FT_Library mLibrary;
    FT_Library getLib();
    Vector<Font*> mActiveFonts;

    // Render state for the font