Various OpenGL optimizations

Change-Id: Ib0742c96f10f5f50e7e5148b742c31b6c232d127
diff --git a/libs/hwui/GradientCache.cpp b/libs/hwui/GradientCache.cpp
index aacf22a..a88a59a 100644
--- a/libs/hwui/GradientCache.cpp
+++ b/libs/hwui/GradientCache.cpp
@@ -171,8 +171,8 @@
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
             GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
 
-    texture->setFilter(GL_LINEAR, GL_LINEAR);
-    texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
+    texture->setFilter(GL_LINEAR);
+    texture->setWrap(GL_CLAMP_TO_EDGE);
 }
 
 }; // namespace uirenderer
diff --git a/libs/hwui/LayerCache.cpp b/libs/hwui/LayerCache.cpp
index 5298125..324765b 100644
--- a/libs/hwui/LayerCache.cpp
+++ b/libs/hwui/LayerCache.cpp
@@ -108,8 +108,8 @@
 
         layer->generateTexture();
         layer->bindTexture();
-        layer->setFilter(GL_NEAREST, GL_NEAREST);
-        layer->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, false);
+        layer->setFilter(GL_NEAREST);
+        layer->setWrap(GL_CLAMP_TO_EDGE, false);
         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 
 #if DEBUG_LAYERS
diff --git a/libs/hwui/LayerRenderer.cpp b/libs/hwui/LayerRenderer.cpp
index 3f940bb..c30923f 100644
--- a/libs/hwui/LayerRenderer.cpp
+++ b/libs/hwui/LayerRenderer.cpp
@@ -458,6 +458,8 @@
         }
 
 error:
+        glEnable(GL_SCISSOR_TEST);
+
 #if DEBUG_OPENGL
         if (error != GL_NO_ERROR) {
             LOGD("GL error while copying layer into bitmap = 0x%x", error);
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index fa17aad..e7eaa50 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -141,13 +141,9 @@
     mFirstSnapshot->height = height;
     mFirstSnapshot->viewport.set(0, 0, width, height);
 
-    mDirtyClip = false;
-
     glDisable(GL_DITHER);
-    glViewport(0, 0, width, height);
-
+    glEnable(GL_SCISSOR_TEST);
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-
     glEnableVertexAttribArray(Program::kBindingPosition);
 }
 
@@ -161,12 +157,13 @@
     mSnapshot = new Snapshot(mFirstSnapshot,
             SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
     mSnapshot->fbo = getTargetFbo();
-
     mSaveCount = 1;
 
-    glEnable(GL_SCISSOR_TEST);
+    glViewport(0, 0, mWidth, mHeight);
     glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
+
     mSnapshot->setClip(left, top, right, bottom);
+    mDirtyClip = false;
 
     if (!opaque) {
         glClear(GL_COLOR_BUFFER_BIT);
@@ -214,13 +211,9 @@
     glEnable(GL_SCISSOR_TEST);
     dirtyClip();
 
-    glDisable(GL_DITHER);
-
     glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 
-    glEnableVertexAttribArray(Program::kBindingPosition);
-
     mCaches.blend = true;
     glEnable(GL_BLEND);
     glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
@@ -916,10 +909,8 @@
         setupDrawProgram();
         setupDrawPureColorUniforms();
         setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
+        setupDrawVertices(&mesh[0].position[0]);
 
-        mCaches.unbindMeshBuffer();
-        glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
-                gVertexStride, &mesh[0].position[0]);
         glDrawArrays(GL_TRIANGLES, 0, count * 6);
 
         glEnable(GL_SCISSOR_TEST);
diff --git a/libs/hwui/TextDropShadowCache.cpp b/libs/hwui/TextDropShadowCache.cpp
index a3ee63b..bbefec6 100644
--- a/libs/hwui/TextDropShadowCache.cpp
+++ b/libs/hwui/TextDropShadowCache.cpp
@@ -137,8 +137,8 @@
         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
                 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
 
-        texture->setFilter(GL_LINEAR, GL_LINEAR);
-        texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
+        texture->setFilter(GL_LINEAR);
+        texture->setWrap(GL_CLAMP_TO_EDGE);
 
         if (size < mMaxSize) {
             if (mDebugEnabled) {
diff --git a/libs/hwui/Texture.h b/libs/hwui/Texture.h
index a4aed07..1adf2c7 100644
--- a/libs/hwui/Texture.h
+++ b/libs/hwui/Texture.h
@@ -49,7 +49,7 @@
             GLenum renderTarget = GL_TEXTURE_2D) {
 
         if (firstWrap || force || wrapS != this->wrapS || wrapT != this->wrapT) {
-            firstWrap = true;
+            firstWrap = false;
 
             this->wrapS = wrapS;
             this->wrapT = wrapT;