Fix OpenGL errors in various places.

Change-Id: I3a4e115d8fb13b6c443e65460d92987b16f2524c
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
index 9c67885..5ef66f3 100644
--- a/libs/hwui/Caches.h
+++ b/libs/hwui/Caches.h
@@ -31,6 +31,7 @@
 #include "ProgramCache.h"
 #include "PathCache.h"
 #include "TextDropShadowCache.h"
+#include "Line.h"
 
 namespace android {
 namespace uirenderer {
@@ -64,6 +65,8 @@
     PatchCache patchCache;
     TextDropShadowCache dropShadowCache;
     GammaFontRenderer fontRenderer;
+
+    Line line;
 }; // class Caches
 
 }; // namespace uirenderer
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 02b1425..102a933 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -133,6 +133,8 @@
 
 OpenGLRenderer::~OpenGLRenderer() {
     LOGD("Destroy OpenGLRenderer");
+    // The context has already been destroyed at this point, do not call
+    // GL APIs. All GL state should be kept in Caches.h
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -615,14 +617,14 @@
     const bool isAA = paint->isAntiAlias();
     if (isAA) {
         GLuint textureUnit = 0;
-        setupTextureAlpha8(mLine.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
-                mode, false, true, mLine.getVertices(), mLine.getTexCoords());
+        setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
+                mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
     } else {
         setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
     }
 
     const float strokeWidth = paint->getStrokeWidth();
-    const GLsizei elementsCount = isAA ? mLine.getElementsCount() : gMeshCount;
+    const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
     const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
 
     for (int i = 0; i < count; i += 4) {
@@ -630,7 +632,7 @@
         float ty = 0.0f;
 
         if (isAA) {
-            mLine.update(points[i], points[i + 1], points[i + 2], points[i + 3],
+            mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
                     strokeWidth, tx, ty);
         } else {
             ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
@@ -647,7 +649,8 @@
         }
         mModelView.translate(tx, ty, 0.0f);
         if (!isAA) {
-            float length = mLine.getLength(points[i], points[i + 1], points[i + 2], points[i + 3]);
+            float length = mCaches.line.getLength(points[i], points[i + 1],
+                    points[i + 2], points[i + 3]);
             mModelView.scale(length, strokeWidth, 1.0f);
         }
         mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
@@ -659,7 +662,9 @@
         glDrawArrays(drawMode, 0, elementsCount);
     }
 
-    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
+    if (isAA) {
+        glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
+    }
 }
 
 void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index eba0f41..f903505 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -40,7 +40,6 @@
 #include "SkiaShader.h"
 #include "SkiaColorFilter.h"
 #include "Caches.h"
-#include "Line.h"
 
 namespace android {
 namespace uirenderer {
@@ -420,9 +419,6 @@
     // List of rectangles to clear due to calls to saveLayer()
     Vector<Rect*> mLayers;
 
-    // Single object used to draw lines
-    Line mLine;
-
     // Misc
     GLint mMaxTextureSize;
 
diff --git a/libs/hwui/SkiaShader.cpp b/libs/hwui/SkiaShader.cpp
index 9e1f6c2..165c0da 100644
--- a/libs/hwui/SkiaShader.cpp
+++ b/libs/hwui/SkiaShader.cpp
@@ -94,6 +94,11 @@
         description.isBitmapNpot = true;
         description.bitmapWrapS = gTileModes[mTileX];
         description.bitmapWrapT = gTileModes[mTileY];
+        mWrapS = GL_CLAMP_TO_EDGE;
+        mWrapT = GL_CLAMP_TO_EDGE;
+    } else {
+        mWrapS = gTileModes[mTileX];
+        mWrapT = gTileModes[mTileY];
     }
 }
 
@@ -121,7 +126,7 @@
     }
 
     // Uniforms
-    bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
+    bindTexture(texture->id, mWrapS, mWrapT, textureSlot);
     glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
     glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
             GL_FALSE, &textureTransform.data[0]);
diff --git a/libs/hwui/SkiaShader.h b/libs/hwui/SkiaShader.h
index 0023c46..9f8778f 100644
--- a/libs/hwui/SkiaShader.h
+++ b/libs/hwui/SkiaShader.h
@@ -122,6 +122,8 @@
 
     SkBitmap* mBitmap;
     const Texture* mTexture;
+    GLenum mWrapS;
+    GLenum mWrapT;
 }; // struct SkiaBitmapShader
 
 /**
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index ca455d8..adf6ee2 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -159,8 +159,8 @@
         return;
     }
 
-    const bool resize = !regenerate || bitmap->width() != texture->width ||
-            bitmap->height() != texture->height;
+    const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
+            bitmap->height() != int(texture->height);
 
     if (!regenerate) {
         glGenTextures(1, &texture->id);