Reduce the number of active texture changes

Change-Id: I94046bdfe20740c26c8183822e3002d692fde7c4
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 27556d9..3c6a952 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -615,6 +615,8 @@
         return;
     }
 
+    Caches& caches = Caches::getInstance();
+    GLuint lastTextureId = 0;
     // Iterate over all the cache lines and see which ones need to be updated
     for (uint32_t i = 0; i < mCacheLines.size(); i++) {
         CacheTextureLine* cl = mCacheLines[i];
@@ -626,7 +628,11 @@
             uint32_t height  = cl->mMaxHeight;
             void* textureData = cacheTexture->mTexture + (yOffset * width);
 
-            glBindTexture(GL_TEXTURE_2D, cacheTexture->mTextureId);
+            if (cacheTexture->mTextureId != lastTextureId) {
+                caches.activeTexture(0);
+                glBindTexture(GL_TEXTURE_2D, cacheTexture->mTextureId);
+                lastTextureId = cacheTexture->mTextureId;
+            }
             glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, width, height,
                     GL_ALPHA, GL_UNSIGNED_BYTE, textureData);
 
@@ -644,6 +650,7 @@
     checkTextureUpdate();
 
     Caches& caches = Caches::getInstance();
+    caches.bindIndicesBuffer(mIndexBufferID);
     if (!mDrawn) {
         float* buffer = mTextMeshPtr;
         int offset = 2;
@@ -654,7 +661,6 @@
                 buffer + offset);
     }
 
-    caches.bindIndicesBuffer(mIndexBufferID);
     glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, NULL);
 
     mDrawn = true;
@@ -779,6 +785,7 @@
         return image;
     }
 
+    mDrawn = false;
     mClip = NULL;
     mBounds = NULL;
 
@@ -806,6 +813,7 @@
     image.image = dataBuffer;
     image.penX = penX;
     image.penY = penY;
+
     return image;
 }
 
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 1f5d13b..cbfd778 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1207,13 +1207,13 @@
 
 void OpenGLRenderer::setupDrawTexture(GLuint texture) {
     bindTexture(texture);
-    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
+    mTextureUnit++;
     mCaches.enableTexCoordsVertexArray();
 }
 
 void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
     bindExternalTexture(texture);
-    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
+    mTextureUnit++;
     mCaches.enableTexCoordsVertexArray();
 }
 
@@ -2128,6 +2128,8 @@
     getAlphaAndMode(paint, &alpha, &mode);
 
     if (mHasShadow) {
+        mCaches.activeTexture(0);
+
         mCaches.dropShadowCache.setFontRenderer(fontRenderer);
         const ShadowTexture* shadow = mCaches.dropShadowCache.get(
                 paint, text, bytesCount, count, mShadowRadius);
@@ -2142,7 +2144,6 @@
             shadowColor = 0xffffffff;
         }
 
-        mCaches.activeTexture(0);
         setupDraw();
         setupDrawWithTexture(true);
         setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
@@ -2158,8 +2159,6 @@
         setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
 
         glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
-
-        finishDrawTexture();
     }
 
     if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp
index cbea843..701314d 100644
--- a/libs/hwui/Program.cpp
+++ b/libs/hwui/Program.cpp
@@ -29,6 +29,7 @@
 Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
     mInitialized = false;
     mHasColorUniform = false;
+    mHasSampler = false;
     mUse = false;
 
     // No need to cache compiled shaders, rely instead on Android's
@@ -176,6 +177,10 @@
 
 void Program::use() {
     glUseProgram(mProgramId);
+    if (texCoords >= 0 && !mHasSampler) {
+        glUniform1i(getUniform("sampler"), 0);
+        mHasSampler = true;
+    }
     mUse = true;
 }
 
diff --git a/libs/hwui/Program.h b/libs/hwui/Program.h
index 559c717..eed909d 100644
--- a/libs/hwui/Program.h
+++ b/libs/hwui/Program.h
@@ -398,6 +398,8 @@
 
     bool mHasColorUniform;
     int mColorUniform;
+
+    bool mHasSampler;
 }; // class Program
 
 }; // namespace uirenderer