Fix and simplify copying texture for popup window.

Change-Id: I3eae4eaae41bb32f08576c484bb8d99b417d9f33
diff --git a/new3d/src/com/android/gallery3d/ui/GLCanvas.java b/new3d/src/com/android/gallery3d/ui/GLCanvas.java
index 78fdfe3..7135a0e 100644
--- a/new3d/src/com/android/gallery3d/ui/GLCanvas.java
+++ b/new3d/src/com/android/gallery3d/ui/GLCanvas.java
@@ -100,14 +100,14 @@
     public void drawMixed(BasicTexture from, BasicTexture to,
             float ratio, int x, int y, int width, int height, float alpha);
 
-    // Copies the specified rectangle to the RawTexture.
-    public void copyTexture2D(
-            RawTexture texture, int x, int y, int width, int height);
+    // Return a texture copied from the specified rectangle.
+    public BasicTexture copyTexture(int x, int y, int width, int height);
 
     // TODO: Remove this or document it.
     public void releaseTextures(Collection<? extends BasicTexture> c);
 
-    // Gets the underlying GL instance. This is used when
+    // Gets the underlying GL instance. This is used only when direct access to
+    // GL is needed.
     public GL11 getGLInstance();
 
     // Binds the texture to the canvas for the following drawing calls. This
diff --git a/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java b/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
index 23e0ba9..eadf43a 100644
--- a/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
+++ b/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
@@ -408,7 +408,7 @@
     }
 
     private void drawBoundTexture(
-            BasicTexture texture, int x, int y, int width, int height) {
+            BasicTexture texture, int x, int y, int width, int height, float alpha) {
         // Test whether it has been rotated or flipped, if so, glDrawTexiOES
         // won't work
         if (isMatrixRotatedOrFlipped(mMatrixValues)) {
@@ -425,7 +425,7 @@
             y = (int) points[1];
             width = (int) points[2] - x;
             height = (int) points[3] - y;
-            mGLState.setTextureAlpha(mAlpha);
+            mGLState.setTextureAlpha(alpha);
             if (width > 0 && height > 0) {
                 ((GL11Ext) mGL).glDrawTexiOES(x, y, 0, width, height);
             }
@@ -444,7 +444,7 @@
 
         mGLState.setBlendEnabled(!texture.isOpaque() || alpha < OPAQUE_ALPHA);
         texture.bind(this);
-        drawBoundTexture(texture, x, y, width, height);
+        drawBoundTexture(texture, x, y, width, height, alpha);
     }
 
     public void drawMixed(BasicTexture from, BasicTexture to,
@@ -501,7 +501,7 @@
         gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_ALPHA, GL11.GL_SRC_ALPHA);
 
         // Draw the combined texture.
-        drawBoundTexture(to, x, y, width, height);
+        drawBoundTexture(to, x, y, width, height, mAlpha);
 
         // Disable TEXTURE1.
         gl.glDisable(GL11.GL_TEXTURE_2D);
@@ -520,8 +520,7 @@
                 || matrix[MSCALE_X] < 0 || matrix[MSCALE_Y] > 0;
     }
 
-    public void copyTexture2D(
-            RawTexture texture, int x, int y, int width, int height) {
+    public BasicTexture copyTexture(int x, int y, int width, int height) {
 
         if (isMatrixRotatedOrFlipped(mMatrixValues)) {
             throw new IllegalArgumentException("cannot support rotated matrix");
@@ -537,6 +536,7 @@
         int newHeight = Util.nextPowerOf2(height);
         int glError = GL11.GL_NO_ERROR;
 
+        RawTexture texture = RawTexture.newInstance(gl);
         gl.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
 
         int[] cropRect = {0,  0, width, height};
@@ -551,7 +551,7 @@
         gl.glTexParameterf(GL11.GL_TEXTURE_2D,
                 GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
         gl.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0,
-                GL11.GL_RGBA, x, y, newWidth, newHeight, 0);
+                GL11.GL_RGB, x, y, newWidth, newHeight, 0);
         glError = gl.glGetError();
 
         if (glError == GL11.GL_OUT_OF_MEMORY) {
@@ -565,6 +565,7 @@
 
         texture.setSize(width, height);
         texture.setTextureSize(newWidth, newHeight);
+        return texture;
     }
 
     private static class GLState {
diff --git a/new3d/src/com/android/gallery3d/ui/PopupWindow.java b/new3d/src/com/android/gallery3d/ui/PopupWindow.java
index 5867433..c08c46d 100644
--- a/new3d/src/com/android/gallery3d/ui/PopupWindow.java
+++ b/new3d/src/com/android/gallery3d/ui/PopupWindow.java
@@ -35,7 +35,6 @@
     protected int mAnchorOffset;
 
     protected int mAnchorPosition;
-    private RawTexture mBackupTexture;
     private GLView mContent;
 
     protected Texture mBackground;
@@ -153,17 +152,12 @@
             mAnchor.draw(canvas, aXoffset, aYoffset);
         }
 
-        GL11 gl = canvas.getGLInstance();
-        if (mBackupTexture == null || mBackupTexture.getBoundGL() != gl) {
-            mBackupTexture = RawTexture.newInstance(gl);
-        }
-
-        RawTexture backup = mBackupTexture;
+        BasicTexture backup = null;
         try {
             // Copy the current drawing results of the triangle area into
             // "backup", so that we can restore the content after it is
             // overlaid by the background.
-            canvas.copyTexture2D(backup, aXoffset, aYoffset, aWidth, aHeight);
+            backup = canvas.copyTexture(aXoffset, aYoffset, aWidth, aHeight);
         } catch (GLOutOfMemoryException e) {
             Log.e(TAG, "out of memory", e);
         }
@@ -173,9 +167,9 @@
                     width, height - aHeight + mAnchorOffset);
         }
 
-        gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ZERO);
-        backup.drawBack(canvas, aXoffset, aYoffset, aWidth, aHeight);
-        gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
+        // restore the backup with alpha = 1
+        canvas.drawTexture(backup, aXoffset, aYoffset, aWidth, aHeight, 1f);
+        backup.deleteFromGL(canvas);
     }
 
     @Override
diff --git a/new3d/src/com/android/gallery3d/ui/RawTexture.java b/new3d/src/com/android/gallery3d/ui/RawTexture.java
index 5c633d4..dffdaa4 100644
--- a/new3d/src/com/android/gallery3d/ui/RawTexture.java
+++ b/new3d/src/com/android/gallery3d/ui/RawTexture.java
@@ -25,10 +25,6 @@
         super(gl, id, STATE_LOADED);
     }
 
-    public GL11 getBoundGL() {
-        return mGL;
-    }
-
     public static RawTexture newInstance(GL11 gl) {
         int[] textureId = new int[1];
         gl.glGenTextures(1, textureId, 0);
@@ -48,11 +44,7 @@
         canvas.bindTexture(this);
     }
 
-    public void drawBack(GLCanvas canvas, int x, int y, int w, int h) {
-        canvas.drawTexture(this, x, y, w, h, 1f);
-    }
-
     public boolean isOpaque() {
-        return false;
+        return true;
     }
 }