Cleanup of FontRenderer

Remove invalid and over complicated buffer unbinding
optimization, and simplify constructor.

Change-Id: I3dd90528de4f377e972bbd7967e7e232875387e4
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 55b2d19..d1d2fcc 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -24,6 +24,7 @@
 #include "Rect.h"
 #include "renderstate/RenderState.h"
 #include "utils/Blur.h"
+#include "utils/MathUtils.h"
 #include "utils/Timing.h"
 
 #include <SkGlyph.h>
@@ -47,7 +48,6 @@
 // TextSetupFunctor
 ///////////////////////////////////////////////////////////////////////////////
 status_t TextSetupFunctor::setup(GLenum glyphFormat) {
-
     renderer->setupDraw();
     renderer->setupDrawTextGamma(paint);
     renderer->setupDrawDirtyRegionsDisabled();
@@ -94,20 +94,23 @@
 
 static bool sLogFontRendererCreate = true;
 
-FontRenderer::FontRenderer() :
-        mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity) {
+FontRenderer::FontRenderer()
+        : mGammaTable(nullptr)
+        , mCurrentFont(nullptr)
+        , mActiveFonts(LruCache<Font::FontDescription, Font*>::kUnlimitedCapacity)
+        , mCurrentCacheTexture(nullptr)
+        , mUploadTexture(false)
+        , mFunctor(nullptr)
+        , mClip(nullptr)
+        , mBounds(nullptr)
+        , mDrawn(false)
+        , mInitialized(false)
+        , mLinearFiltering(false) {
 
     if (sLogFontRendererCreate) {
         INIT_LOGD("Creating FontRenderer");
     }
 
-    mGammaTable = nullptr;
-    mInitialized = false;
-
-    mCurrentCacheTexture = nullptr;
-
-    mLinearFiltering = false;
-
     mSmallCacheWidth = DEFAULT_TEXT_SMALL_CACHE_WIDTH;
     mSmallCacheHeight = DEFAULT_TEXT_SMALL_CACHE_HEIGHT;
     mLargeCacheWidth = DEFAULT_TEXT_LARGE_CACHE_WIDTH;
@@ -131,10 +134,11 @@
     }
 
     uint32_t maxTextureSize = (uint32_t) Caches::getInstance().maxTextureSize;
-    mSmallCacheWidth = mSmallCacheWidth > maxTextureSize ? maxTextureSize : mSmallCacheWidth;
-    mSmallCacheHeight = mSmallCacheHeight > maxTextureSize ? maxTextureSize : mSmallCacheHeight;
-    mLargeCacheWidth = mLargeCacheWidth > maxTextureSize ? maxTextureSize : mLargeCacheWidth;
-    mLargeCacheHeight = mLargeCacheHeight > maxTextureSize ? maxTextureSize : mLargeCacheHeight;
+
+    mSmallCacheWidth = MathUtils::min(mSmallCacheWidth, maxTextureSize);
+    mSmallCacheHeight = MathUtils::min(mSmallCacheHeight, maxTextureSize);
+    mLargeCacheWidth = MathUtils::min(mLargeCacheWidth, maxTextureSize);
+    mLargeCacheHeight = MathUtils::min(mLargeCacheHeight, maxTextureSize);
 
     if (sLogFontRendererCreate) {
         INIT_LOGD("  Text cache sizes, in pixels: %i x %i, %i x %i, %i x %i, %i x %i",
@@ -493,22 +497,19 @@
         CacheTexture* texture = cacheTextures[i];
         if (texture->canDraw()) {
             if (first) {
-                if (mFunctor) {
-                    mFunctor->setup(texture->getFormat());
-                }
+                mFunctor->setup(texture->getFormat());
 
                 checkTextureUpdate();
                 renderState.meshState().bindQuadIndicesBuffer();
 
-                if (!mDrawn) {
-                    // If returns true, a VBO was bound and we must
-                    // rebind our vertex attrib pointers even if
-                    // they have the same values as the current pointers
-                    forceRebind = renderState.meshState().unbindMeshBuffer();
-                }
+                // If returns true, a VBO was bound and we must
+                // rebind our vertex attrib pointers even if
+                // they have the same values as the current pointers
+                forceRebind = renderState.meshState().unbindMeshBuffer();
 
                 caches.textureState().activateTexture(0);
                 first = false;
+                mDrawn = true;
             }
 
             caches.textureState().bindTexture(texture->getTextureId());
@@ -531,8 +532,6 @@
 void FontRenderer::issueDrawCommand() {
     issueDrawCommand(mACacheTextures);
     issueDrawCommand(mRGBACacheTextures);
-
-    mDrawn = true;
 }
 
 void FontRenderer::appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index cef3326..dd5c49f 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1169,7 +1169,7 @@
 }
 
 void OpenGLRenderer::dirtyLayer(const float left, const float top,
-        const float right, const float bottom, const mat4 transform) {
+        const float right, const float bottom, const Matrix4& transform) {
     if (hasLayer()) {
         Rect bounds(left, top, right, bottom);
         transform.mapRect(bounds);
@@ -2805,16 +2805,11 @@
 
     TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
     if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
-            positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
-        if (hasActiveLayer) {
-            if (!pureTranslate) {
-                currentTransform()->mapRect(bounds);
-            }
-            dirtyLayerUnchecked(bounds, getRegion());
-        }
+            positions, hasLayer() ? &bounds : nullptr, &functor)) {
+        dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
+        mDirty = true;
     }
 
-    mDirty = true;
 }
 
 bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
@@ -3011,13 +3006,9 @@
 
     if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
             hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
-        if (hasActiveLayer) {
-            currentTransform()->mapRect(bounds);
-            dirtyLayerUnchecked(bounds, getRegion());
-        }
+        dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
+        mDirty = true;
     }
-
-    mDirty = true;
 }
 
 void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
@@ -3109,11 +3100,9 @@
                             x + layer->layer.getWidth(), y + layer->layer.getHeight());
                 }
 
-
                 TextureVertex* mesh = &layer->mesh[0];
                 GLsizei elementsCount = layer->meshElementCount;
 
-
                 while (elementsCount > 0) {
                     GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
 
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 63a63b8..e1bef65 100755
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -677,7 +677,7 @@
      * are transformed with the supplied matrix.
      */
     void dirtyLayer(const float left, const float top,
-            const float right, const float bottom, const mat4 transform);
+            const float right, const float bottom, const Matrix4& transform);
 
     /**
      * Mark the layer as dirty at the specified coordinates.