Fix gradients rendering and destructor crashes.

This changes binds all textures to GL_TEXTURE0, this will have
to be changed when combining shader capabilities.

Change-Id: I02df4f5ba41e9b01ffa52fd7c26b41477c7ed18f
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 6074ec8..5595ab0 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -327,7 +327,6 @@
     mTextTexture = new unsigned char[mCacheWidth * mCacheHeight];
     mUploadTexture = false;
 
-    glActiveTexture(GL_TEXTURE0);
     glGenTextures(1, &mTextureId);
     glBindTexture(GL_TEXTURE_2D, mTextureId);
     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
diff --git a/libs/hwui/GenerationCache.h b/libs/hwui/GenerationCache.h
index 5c1b5e1..45b3ffa 100644
--- a/libs/hwui/GenerationCache.h
+++ b/libs/hwui/GenerationCache.h
@@ -34,13 +34,12 @@
 struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
     Entry() { }
     Entry(const Entry<EntryKey, EntryValue>& e):
-            key(e.key), value(e.value), index(e.index), parent(e.parent), child(e.child) { }
+            key(e.key), value(e.value), parent(e.parent), child(e.child) { }
     Entry(sp<Entry<EntryKey, EntryValue> > e):
-            key(e->key), value(e->value), index(e->index), parent(e->parent), child(e->child) { }
+            key(e->key), value(e->value), parent(e->parent), child(e->child) { }
 
     EntryKey key;
     EntryValue value;
-    ssize_t index;
 
     sp<Entry<EntryKey, EntryValue> > parent;
     sp<Entry<EntryKey, EntryValue> > child;
@@ -156,7 +155,7 @@
 void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) {
     entry->key = key;
     entry->value = value;
-    entry->index = mCache.add(key, entry);
+    mCache.add(key, entry);
     attachToCache(entry);
 }
 
@@ -185,7 +184,10 @@
 template<typename K, typename V>
 V GenerationCache<K, V>::removeOldest() {
     if (mOldest.get()) {
-        return removeAt(mOldest->index);
+        ssize_t index = mCache.indexOfKey(mOldest->key);
+        if (index >= 0) {
+            return removeAt(index);
+        }
     }
 
     return NULL;
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 3d9aa26..3c36f95 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -144,11 +144,13 @@
 
     memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
 
-    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mMaxTextureUnits);
-    if (mMaxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
+    mFirstSnapshot = new Snapshot;
+
+    GLint maxTextureUnits;
+    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
+    if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
         LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
     }
-    mLastTexture[0] = mLastTexture[1] = mLastTexture[2] = 0;
 }
 
 OpenGLRenderer::~OpenGLRenderer() {
@@ -171,11 +173,11 @@
 
     mWidth = width;
     mHeight = height;
-    mFirstSnapshot.height = height;
+    mFirstSnapshot->height = height;
 }
 
 void OpenGLRenderer::prepare() {
-    mSnapshot = &mFirstSnapshot;
+    mSnapshot = mFirstSnapshot;
     mSaveCount = 0;
 
     glDisable(GL_SCISSOR_TEST);
@@ -265,10 +267,6 @@
     glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
 
     Layer* layer = current->layer;
-
-    // Compute the correct texture coordinates for the FBO texture
-    // The texture is currently as big as the window but drawn with
-    // a quad of the appropriate size
     const Rect& rect = layer->layer;
 
     drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
@@ -323,7 +321,6 @@
 
 bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
         float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
-
     LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
     LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
 
@@ -537,8 +534,6 @@
         return;
     }
 
-    glActiveTexture(GL_TEXTURE0);
-
     float length;
     switch (paint->getTextAlign()) {
         case SkPaint::kCenter_Align:
@@ -687,7 +682,6 @@
 
 void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
         float alpha, SkXfermode::Mode mode) {
-    glActiveTexture(GL_TEXTURE1);
     Texture* texture = mGradientCache.get(mShaderKey);
     if (!texture) {
         SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
@@ -714,8 +708,8 @@
     mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
 
     chooseBlending(mShaderBlend || alpha < 1.0f, mode);
-    bindTexture(texture->id, mShaderTileX, mShaderTileY, 1);
-    glUniform1i(mCurrentProgram->getUniform("gradientSampler"), 1);
+    bindTexture(texture->id, mShaderTileX, mShaderTileY, 0);
+    glUniform1i(mCurrentProgram->getUniform("gradientSampler"), 0);
 
     Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
     if (mShaderMatrix) {
@@ -747,7 +741,6 @@
 
 void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
         float alpha, SkXfermode::Mode mode) {
-    glActiveTexture(GL_TEXTURE2);
     const Texture* texture = mTextureCache.get(mShaderBitmap);
 
     const float width = texture->width;
@@ -782,8 +775,8 @@
     chooseBlending(texture->blend || alpha < 1.0f, mode);
 
     // Texture
-    bindTexture(texture->id, mShaderTileX, mShaderTileY, 2);
-    glUniform1i(mCurrentProgram->getUniform("bitmapSampler"), 2);
+    bindTexture(texture->id, mShaderTileX, mShaderTileY, 0);
+    glUniform1i(mCurrentProgram->getUniform("bitmapSampler"), 0);
     glUniformMatrix4fv(mCurrentProgram->getUniform("textureTransform"), 1,
             GL_FALSE, &textureTransform.data[0]);
     glUniform2f(mCurrentProgram->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
@@ -917,10 +910,7 @@
 
 void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
     glActiveTexture(gTextureUnits[textureUnit]);
-    if (texture != mLastTexture[textureUnit]) {
-        glBindTexture(GL_TEXTURE_2D, texture);
-        mLastTexture[textureUnit] = texture;
-    }
+    glBindTexture(GL_TEXTURE_2D, texture);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
 }
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 5e5c021..937ff08 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -315,7 +315,7 @@
     // Number of saved states
     int mSaveCount;
     // Base state
-    Snapshot mFirstSnapshot;
+    sp<Snapshot> mFirstSnapshot;
     // Current state
     sp<Snapshot> mSnapshot;
 
@@ -325,10 +325,6 @@
     // Used to draw textured quads
     TextureVertex mMeshVertices[4];
 
-    // Current texture state
-    GLuint mLastTexture[REQUIRED_TEXTURE_UNITS_COUNT];
-    GLint mMaxTextureUnits;
-
     // Last known blend state
     bool mBlend;
     GLenum mLastSrcMode;
diff --git a/libs/hwui/Snapshot.h b/libs/hwui/Snapshot.h
index cc7fde9..020bdb0 100644
--- a/libs/hwui/Snapshot.h
+++ b/libs/hwui/Snapshot.h
@@ -50,7 +50,7 @@
      * assumed to be dirty. The specified snapshot is stored as the previous
      * snapshot.
      */
-    Snapshot(const sp<Snapshot> s):
+    Snapshot(const sp<Snapshot>& s):
             height(s->height),
             transform(s->transform),
             clipRect(s->clipRect),
diff --git a/tests/HwAccelerationTest/src/com/google/android/test/hwui/ShadersActivity.java b/tests/HwAccelerationTest/src/com/google/android/test/hwui/ShadersActivity.java
index abd741c..0cd1426 100644
--- a/tests/HwAccelerationTest/src/com/google/android/test/hwui/ShadersActivity.java
+++ b/tests/HwAccelerationTest/src/com/google/android/test/hwui/ShadersActivity.java
@@ -78,7 +78,7 @@
             mScaledShader.setLocalMatrix(m2);
 
             mHorGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth, 0.0f,
-                    Color.RED, Color.GREEN, Shader.TileMode.REPEAT);
+                    Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
             
             mDiagGradient = new LinearGradient(0.0f, 0.0f, mDrawWidth / 1.5f, mDrawHeight,
                     Color.BLUE, Color.MAGENTA, Shader.TileMode.CLAMP);