Refactor the code of mixed texture. Remove the class.

Change-Id: I33d3fba34f34372a6694068d4189bb82f308150c
diff --git a/new3d/src/com/android/gallery3d/ui/AdaptiveBackground.java b/new3d/src/com/android/gallery3d/ui/AdaptiveBackground.java
index 5f9b74d..71731c7 100644
--- a/new3d/src/com/android/gallery3d/ui/AdaptiveBackground.java
+++ b/new3d/src/com/android/gallery3d/ui/AdaptiveBackground.java
@@ -16,13 +16,13 @@
 
 package com.android.gallery3d.ui;
 
-import com.android.gallery3d.anim.FloatAnimation;
-
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.LightingColorFilter;
 import android.graphics.Paint;
 
+import com.android.gallery3d.anim.FloatAnimation;
+
 public class AdaptiveBackground extends GLView {
 
     private static final int BACKGROUND_WIDTH = 128;
@@ -30,7 +30,9 @@
     private static final int FILTERED_COLOR = 0xffaaaaaa;
     private static final int ANIMATION_DURATION = 500;
 
-    private MixedTexture mMixedTexture;
+    private BasicTexture mOldBackground;
+    private BasicTexture mBackground;
+
     private final Paint mPaint;
     private Bitmap mPendingBitmap;
     private final FloatAnimation mAnimation =
@@ -68,13 +70,12 @@
 
     private void startTransition(Bitmap bitmap) {
         BitmapTexture texture = new BitmapTexture(bitmap);
-        if (mMixedTexture == null) {
-            mMixedTexture = new MixedTexture(texture);
+        if (mBackground == null) {
+            mBackground = texture;
         } else {
-            mMixedTexture.setNewDestination(texture);
-        }
-        mMixedTexture.setMixtureRatio(0);
-        if (mMixedTexture.hasSource()) {
+            if (mOldBackground != null) mOldBackground.recycle();
+            mOldBackground = mBackground;
+            mBackground = texture;
             mAnimation.start();
         }
         invalidate();
@@ -96,15 +97,7 @@
 
     @Override
     protected void render(GLCanvas canvas) {
-        if (mMixedTexture == null) return;
-
-        if (mAnimation.calculate(canvas.currentAnimationTimeMillis())) {
-            mMixedTexture.setMixtureRatio(mAnimation.get());
-            invalidate();
-        } else if (mPendingBitmap != null) {
-            startTransition(mPendingBitmap);
-            mPendingBitmap = null;
-        }
+        if (mBackground == null) return;
 
         int height = getHeight();
         float scale = (float) height / BACKGROUND_HEIGHT;
@@ -112,9 +105,24 @@
         int scroll = mScrollX;
         int start = (scroll / width) * width;
 
-        MixedTexture mixed = mMixedTexture;
-        for (int i = start, n = scroll + getWidth(); i < n; i += width) {
-            mMixedTexture.draw(canvas, i - scroll, 0, width, height);
+        if (mOldBackground == null) {
+            for (int i = start, n = scroll + getWidth(); i < n; i += width) {
+                mBackground.draw(canvas, i - scroll, 0, width, height);
+            }
+        } else {
+            boolean moreAnimation =
+                    mAnimation.calculate(canvas.currentAnimationTimeMillis());
+            float ratio = mAnimation.get();
+            for (int i = start, n = scroll + getWidth(); i < n; i += width) {
+                canvas.drawMixed(mOldBackground,
+                        mBackground, ratio, i - scroll, 0, width, height);
+            }
+            if (moreAnimation) {
+                invalidate();
+            } else if (mPendingBitmap != null) {
+                mPendingBitmap = null;
+                startTransition(mPendingBitmap);
+            }
         }
     }
 }
diff --git a/new3d/src/com/android/gallery3d/ui/GLCanvas.java b/new3d/src/com/android/gallery3d/ui/GLCanvas.java
index 015bde4..5b64da1 100644
--- a/new3d/src/com/android/gallery3d/ui/GLCanvas.java
+++ b/new3d/src/com/android/gallery3d/ui/GLCanvas.java
@@ -101,12 +101,10 @@
 
     // Draw two textures to the specified rectange. The actual texture used is
     // from * (1 - ratio) + to * ratio
+    // The two textures must have the same size.
     public void drawMixed(BasicTexture from, BasicTexture to,
             float ratio, int x, int y, int w, int h);
 
-    public void drawMixed(BasicTexture from, BasicTexture to,
-            float ratio, int x, int y, int width, int height, float alpha);
-
     // Return a texture copied from the specified rectangle.
     public BasicTexture copyTexture(int x, int y, int width, int height);
 
diff --git a/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java b/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
index aeaccd1..c70bfe7 100644
--- a/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
+++ b/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
@@ -537,8 +537,22 @@
         color[3] = alpha;
     }
 
-    public void drawMixed(BasicTexture from, BasicTexture to,
+    private void drawMixed(BasicTexture from, BasicTexture to,
             float ratio, int x, int y, int width, int height, float alpha) {
+
+        if (ratio <= 0) {
+            drawTexture(from, x, y, width, height, alpha);
+            return;
+        } else if (ratio >= 1) {
+            drawTexture(to, x, y, width, height, alpha);
+            return;
+        }
+
+        // In the current implementation the two textures must have the
+        // same size.
+        Util.Assert(from.getWidth() == to.getWidth()
+                && from.getHeight() == to.getHeight());
+
         mGLState.setBlendEnabled(!from.isOpaque()
                 || !to.isOpaque() || alpha < OPAQUE_ALPHA);
 
diff --git a/new3d/src/com/android/gallery3d/ui/MixedTexture.java b/new3d/src/com/android/gallery3d/ui/MixedTexture.java
deleted file mode 100644
index 9b69a87..0000000
--- a/new3d/src/com/android/gallery3d/ui/MixedTexture.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.gallery3d.ui;
-
-// MixedTexture is a texture whose color is mixed from two textures: source
-// and destination:
-//   color = (1 - r) * source + r * destination
-// The mix ratio r is set by setMixtureRatio().
-// The two textures must have the same size.
-//
-// Initially only the destination texture is set in the constructor, and there
-// is no source texture. When setNewDestination() is called, the original
-// destination texture becomes source texture, and the new destination texture
-// is set.
-//
-// If there is no source texture, hasSource() returns false, and draw() just
-// draws the destination texture.
-public class MixedTexture implements Texture {
-    private BasicTexture mSource;
-    private BasicTexture mDestination;
-    private float mMixRatio = 1.0f;
-    private final int mWidth;
-    private final int mHeight;
-
-    public MixedTexture(BasicTexture texture) {
-        mDestination = texture;
-        mWidth = texture.getWidth();
-        mHeight = texture.getHeight();
-    }
-
-    public BasicTexture setNewDestination(BasicTexture texture) {
-        if (texture.getWidth() != mWidth || texture.getHeight() != mHeight) {
-            throw new IllegalArgumentException();
-        }
-        BasicTexture result = mSource;
-        mSource = mDestination;
-        mDestination = texture;
-        return result;
-    }
-
-    public void setMixtureRatio(float ratio) {
-        if (ratio > 1 || ratio < 0) {
-            throw new IllegalArgumentException();
-        }
-        mMixRatio = ratio;
-    }
-
-    public void draw(GLCanvas canvas, int x, int y) {
-        draw(canvas, x, y, mWidth, mHeight);
-    }
-
-    public void draw(GLCanvas canvas, int x, int y, int w, int h) {
-        if (mMixRatio >= 1 || mSource == null) {
-            mDestination.draw(canvas, x, y, w, h);
-        } else if (mMixRatio <= 0) {
-            mSource.draw(canvas, x, y, w, h);
-        } else {
-            canvas.drawMixed(mSource, mDestination, mMixRatio, x, y, w, h);
-        }
-    }
-
-    public boolean hasSource() {
-        return mSource != null;
-    }
-
-    public int getWidth() {
-        return mWidth;
-    }
-
-    public int getHeight() {
-        return mHeight;
-    }
-
-    public boolean isOpaque() {
-        return true;
-    }
-}
diff --git a/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java b/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
index 20b4f55..5f0ea80 100644
--- a/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
+++ b/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
@@ -639,6 +639,9 @@
     private static class DrawTextureMixedTest extends GLMock {
 
         boolean mTexture2DEnabled0, mTexture2DEnabled1;
+        int mBindTexture0;
+        int mBindTexture1;
+
         @Override
         public void glEnable(int cap) {
             if (cap == GL_TEXTURE_2D) {
@@ -674,9 +677,9 @@
         public void glBindTexture(int target, int texture) {
             if (target == GL_TEXTURE_2D) {
                 if (mGLActiveTexture == GL_TEXTURE0) {
-                    assertEquals(42, texture);
+                    mBindTexture0 = texture;
                 } else if (mGLActiveTexture == GL_TEXTURE1) {
-                    assertEquals(47, texture);
+                    mBindTexture1 = texture;
                 } else {
                     fail();
                 }
@@ -689,21 +692,31 @@
             MyTexture from = new MyTexture(canvas, 42, false);  // non-opaque
             MyTexture to = new MyTexture(canvas, 47, true);  // opaque
 
-            canvas.drawMixed(from, to, 0.5f, 100, 200, 300, 400, 1.0f);
-            assertEquals(GL_COMBINE, getTexEnvi(GL_TEXTURE_ENV_MODE));
-            assertEquals(GL_INTERPOLATE, getTexEnvi(GL_COMBINE_RGB));
-            assertEquals(GL_INTERPOLATE, getTexEnvi(GL_COMBINE_ALPHA));
-            assertEquals(GL_CONSTANT, getTexEnvi(GL_SRC2_RGB));
-            assertEquals(GL_CONSTANT, getTexEnvi(GL_SRC2_ALPHA));
-            assertEquals(GL_SRC_ALPHA, getTexEnvi(GL_OPERAND2_RGB));
-            assertEquals(GL_SRC_ALPHA, getTexEnvi(GL_OPERAND2_ALPHA));
+            canvas.drawMixed(from, to, 0.5f, 100, 200, 300, 400);
+            assertEquals(42, mBindTexture0);
+            assertEquals(47, mBindTexture1);
             assertTrue(mTexture2DEnabled0);
             assertFalse(mTexture2DEnabled1);
+
+            assertEquals(GL_COMBINE, getTexEnvi(GL_TEXTURE1, GL_TEXTURE_ENV_MODE));
+            assertEquals(GL_INTERPOLATE, getTexEnvi(GL_TEXTURE1, GL_COMBINE_RGB));
+            assertEquals(GL_INTERPOLATE, getTexEnvi(GL_TEXTURE1, GL_COMBINE_ALPHA));
+            assertEquals(GL_CONSTANT, getTexEnvi(GL_TEXTURE1, GL_SRC2_RGB));
+            assertEquals(GL_CONSTANT, getTexEnvi(GL_TEXTURE1, GL_SRC2_ALPHA));
+            assertEquals(GL_SRC_ALPHA, getTexEnvi(GL_TEXTURE1, GL_OPERAND2_RGB));
+            assertEquals(GL_SRC_ALPHA, getTexEnvi(GL_TEXTURE1, GL_OPERAND2_ALPHA));
+
+            assertEquals(GL_REPLACE, getTexEnvi(GL_TEXTURE0, GL_TEXTURE_ENV_MODE));
+
             assertFalse(mGLBlendEnabled);
 
-            canvas.setAlpha(0.3f);
-            canvas.drawMixed(from, to, 0.5f, 100, 200, 300, 400, 1.0f);
-            assertEquals(GL_COMBINE, getTexEnvi(GL_TEXTURE_ENV_MODE));
+            canvas.drawMixed(from, to, 0, 100, 200, 300, 400);
+            assertEquals(GL_REPLACE, getTexEnvi(GL_TEXTURE0, GL_TEXTURE_ENV_MODE));
+            assertEquals(42, mBindTexture0);
+
+            canvas.drawMixed(from, to, 1, 100, 200, 300, 400);
+            assertEquals(GL_REPLACE, getTexEnvi(GL_TEXTURE0, GL_TEXTURE_ENV_MODE));
+            assertEquals(47, mBindTexture0);
         }
     }
 
diff --git a/new3d/tests/src/com/android/gallery3d/ui/GLMock.java b/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
index b92787e..4d59a29 100644
--- a/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
+++ b/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
@@ -1,16 +1,10 @@
 package com.android.gallery3d.ui;
 
-import android.util.Log;
-
 import java.nio.Buffer;
 import java.util.HashMap;
 
-import javax.microedition.khronos.opengles.GL;
 import javax.microedition.khronos.opengles.GL10;
-import javax.microedition.khronos.opengles.GL10Ext;
 import javax.microedition.khronos.opengles.GL11;
-import javax.microedition.khronos.opengles.GL11Ext;
-import javax.microedition.khronos.opengles.GL11ExtensionPack;
 
 public class GLMock extends GLStub {
     private static final String TAG = "GLMock";
@@ -40,7 +34,8 @@
     // glBindTexture
     int mGLBindTextureId;
     // glTexEnvf
-    HashMap<Integer, Float> mGLTexEnv = new HashMap<Integer, Float>();
+    HashMap<Integer, Float> mGLTexEnv0 = new HashMap<Integer, Float>();
+    HashMap<Integer, Float> mGLTexEnv1 = new HashMap<Integer, Float>();
     // glActiveTexture
     int mGLActiveTexture = GL11.GL_TEXTURE0;
 
@@ -140,12 +135,28 @@
     @Override
     public void glTexEnvf(int target, int pname, float param) {
         if (target == GL11.GL_TEXTURE_ENV) {
-            mGLTexEnv.put(pname, param);
+            if (mGLActiveTexture == GL11.GL_TEXTURE0) {
+                mGLTexEnv0.put(pname, param);
+            } else if (mGLActiveTexture == GL11.GL_TEXTURE1) {
+                mGLTexEnv1.put(pname, param);
+            } else {
+                throw new AssertionError();
+            }
         }
     }
 
     public int getTexEnvi(int pname) {
-        return (int) mGLTexEnv.get(pname).floatValue();
+        return getTexEnvi(mGLActiveTexture, pname);
+    }
+
+    public int getTexEnvi(int activeTexture, int pname) {
+        if (activeTexture == GL11.GL_TEXTURE0) {
+            return (int) mGLTexEnv0.get(pname).floatValue();
+        } else if (activeTexture == GL11.GL_TEXTURE1) {
+            return (int) mGLTexEnv1.get(pname).floatValue();
+        } else {
+            throw new AssertionError();
+        }
     }
 
     @Override
diff --git a/new3d/tests/src/com/android/gallery3d/ui/TextureTest.java b/new3d/tests/src/com/android/gallery3d/ui/TextureTest.java
index 2fd5e7d..fcc4a6c 100644
--- a/new3d/tests/src/com/android/gallery3d/ui/TextureTest.java
+++ b/new3d/tests/src/com/android/gallery3d/ui/TextureTest.java
@@ -176,43 +176,6 @@
     }
 
     @SmallTest
-    public void testMixedTexture() {
-        GL11 glStub = new GLStub();
-        GLCanvasMock canvas = new GLCanvasMock(glStub);
-        MyTextureForMixed texture1 = new MyTextureForMixed(canvas, 47);
-        MyTextureForMixed texture2 = new MyTextureForMixed(canvas, 42);
-
-        MixedTexture texture = new MixedTexture(texture1);
-        assertFalse(texture.hasSource());
-        texture.draw(canvas, 0, 0);
-        assertEquals(0, canvas.mDrawMixedCalled);
-        assertEquals(1, canvas.mDrawTextureCalled);
-
-        texture.setMixtureRatio(0.5f);
-        texture.setNewDestination(texture2);
-        assertTrue(texture.hasSource());
-        texture.draw(canvas, 0, 0);
-        assertEquals(1, canvas.mDrawMixedCalled);
-        assertEquals(1, canvas.mDrawTextureCalled);
-
-        texture.setMixtureRatio(0.3f);
-        texture.draw(canvas, 0, 0);
-        assertEquals(0.3f, canvas.mDrawMixedRatio);
-        assertEquals(2, canvas.mDrawMixedCalled);
-        assertEquals(1, canvas.mDrawTextureCalled);
-
-        texture.setMixtureRatio(0f);
-        texture.draw(canvas, 0, 0);
-        assertEquals(2, canvas.mDrawMixedCalled);
-        assertEquals(2, canvas.mDrawTextureCalled);
-
-        texture.setMixtureRatio(1f);
-        texture.draw(canvas, 0, 0);
-        assertEquals(2, canvas.mDrawMixedCalled);
-        assertEquals(3, canvas.mDrawTextureCalled);
-    }
-
-    @SmallTest
     public void testBitmapTexture() {
         Config config = Config.ARGB_8888;
         Bitmap bitmap = Bitmap.createBitmap(47, 42, config);