From e3a9b24b5e3f9b2058486814a6d27729e51ad466 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Tue, 8 Jan 2013 11:15:30 -0800 Subject: Add plumbing for better text scaling Fonts are now described by a transform matrix. This lead to switching from a vector to a hashmap. This change therefore adds new comparators and hash computations to Font. Change-Id: I2daffa7d6287c18554c606b8bfa06640d28b4530 --- libs/hwui/FontRenderer.cpp | 78 +++++++++++----------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) (limited to 'libs/hwui/FontRenderer.cpp') diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp index 47784a41dcae..5c1eb38786a2 100644 --- a/libs/hwui/FontRenderer.cpp +++ b/libs/hwui/FontRenderer.cpp @@ -36,7 +36,9 @@ namespace uirenderer { static bool sLogFontRendererCreate = true; -FontRenderer::FontRenderer() { +FontRenderer::FontRenderer() : + mActiveFonts(LruCache::kUnlimitedCapacity) { + if (sLogFontRendererCreate) { INIT_LOGD("Creating FontRenderer"); } @@ -107,10 +109,11 @@ FontRenderer::~FontRenderer() { delete[] mTextMesh; } - Vector fontsToDereference = mActiveFonts; - for (uint32_t i = 0; i < fontsToDereference.size(); i++) { - delete fontsToDereference[i]; + LruCache::Iterator it(mActiveFonts); + while (it.next()) { + delete it.value(); } + mActiveFonts.clear(); } void FontRenderer::flushAllAndInvalidate() { @@ -118,8 +121,9 @@ void FontRenderer::flushAllAndInvalidate() { issueDrawCommand(); } - for (uint32_t i = 0; i < mActiveFonts.size(); i++) { - mActiveFonts[i]->invalidateTextureCache(); + LruCache::Iterator it(mActiveFonts); + while (it.next()) { + it.value()->invalidateTextureCache(); } for (uint32_t i = 0; i < mCacheTextures.size(); i++) { @@ -146,8 +150,9 @@ void FontRenderer::flushLargeCaches() { CacheTexture* cacheTexture = mCacheTextures[i]; if (cacheTexture->getTexture()) { cacheTexture->init(); - for (uint32_t j = 0; j < mActiveFonts.size(); j++) { - mActiveFonts[j]->invalidateTextureCache(cacheTexture); + LruCache::Iterator it(mActiveFonts); + while (it.next()) { + it.value()->invalidateTextureCache(cacheTexture); } cacheTexture->releaseTexture(); } @@ -480,22 +485,8 @@ void FontRenderer::appendRotatedMeshQuad(float x1, float y1, float u1, float v1, } } -void FontRenderer::setFont(SkPaint* paint, uint32_t fontId, float fontSize) { - int flags = 0; - if (paint->isFakeBoldText()) { - flags |= Font::kFakeBold; - } - - const float skewX = paint->getTextSkewX(); - uint32_t italicStyle = *(uint32_t*) &skewX; - const float scaleXFloat = paint->getTextScaleX(); - uint32_t scaleX = *(uint32_t*) &scaleXFloat; - SkPaint::Style style = paint->getStyle(); - const float strokeWidthFloat = paint->getStrokeWidth(); - uint32_t strokeWidth = *(uint32_t*) &strokeWidthFloat; - mCurrentFont = Font::create(this, fontId, fontSize, flags, italicStyle, - scaleX, style, strokeWidth); - +void FontRenderer::setFont(SkPaint* paint, const mat4& matrix) { + mCurrentFont = Font::create(this, paint, matrix); } FontRenderer::DropShadow FontRenderer::renderDropShadow(SkPaint* paint, const char *text, @@ -561,39 +552,11 @@ void FontRenderer::finishRender() { } } -void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs) { - int flags = 0; - if (paint->isFakeBoldText()) { - flags |= Font::kFakeBold; - } - const float skewX = paint->getTextSkewX(); - uint32_t italicStyle = *(uint32_t*) &skewX; - const float scaleXFloat = paint->getTextScaleX(); - uint32_t scaleX = *(uint32_t*) &scaleXFloat; - SkPaint::Style style = paint->getStyle(); - const float strokeWidthFloat = paint->getStrokeWidth(); - uint32_t strokeWidth = *(uint32_t*) &strokeWidthFloat; - float fontSize = paint->getTextSize(); - Font* font = Font::create(this, SkTypeface::UniqueID(paint->getTypeface()), - fontSize, flags, italicStyle, scaleX, style, strokeWidth); - +void FontRenderer::precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix) { + Font* font = Font::create(this, paint, matrix); font->precache(paint, text, numGlyphs); } -bool FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text, - uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, Rect* bounds) { - if (!mCurrentFont) { - ALOGE("No font set"); - return false; - } - - initRender(clip, bounds); - mCurrentFont->render(paint, text, startIndex, len, numGlyphs, x, y); - finishRender(); - - return mDrawn; -} - bool FontRenderer::renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds) { @@ -625,12 +588,7 @@ bool FontRenderer::renderTextOnPath(SkPaint* paint, const Rect* clip, const char } void FontRenderer::removeFont(const Font* font) { - for (uint32_t ct = 0; ct < mActiveFonts.size(); ct++) { - if (mActiveFonts[ct] == font) { - mActiveFonts.removeAt(ct); - break; - } - } + mActiveFonts.remove(font->getDescription()); if (mCurrentFont == font) { mCurrentFont = NULL; -- cgit v1.2.3-59-g8ed1b