Clear the color buffer only when needed.
Also other small tweaks to speed up rendering.

Change-Id: Iab4721b7e9eee1391cc1d7b0594eabe20f5f3434
diff --git a/new3d/src/com/android/gallery3d/ui/CanvasTexture.java b/new3d/src/com/android/gallery3d/ui/CanvasTexture.java
index 6dcc519..b9846ba 100644
--- a/new3d/src/com/android/gallery3d/ui/CanvasTexture.java
+++ b/new3d/src/com/android/gallery3d/ui/CanvasTexture.java
@@ -23,10 +23,11 @@
 /** Using a canvas to draw the texture */
 abstract class CanvasTexture extends UploadedTexture {
     protected Canvas mCanvas;
-    private final Config mConfig = Config.ARGB_8888;
+    private final Config mConfig;
 
     public CanvasTexture(int width, int height, Config config) {
         setSize(width, height);
+        mConfig = config;
     }
 
     public CanvasTexture(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 ed43e84..9263a93 100644
--- a/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
+++ b/new3d/src/com/android/gallery3d/ui/GLCanvasImp.java
@@ -4,6 +4,7 @@
 import android.graphics.RectF;
 import android.opengl.GLU;
 import android.opengl.Matrix;
+import android.util.Log;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -65,8 +66,6 @@
         initialize();
     }
 
-    private int mClipRetryCount = 0;
-
     public void setSize(int width, int height) {
         Util.Assert(width >= 0 && height >= 0);
 
@@ -88,7 +87,7 @@
         Matrix.scaleM(matrix, 0, 1, -1, 1);
 
         mClipRect.set(0, 0, width, height);
-        mClipRetryCount = 2;
+        gl.glScissor(0, 0, width, height);
     }
 
     public long currentAnimationTimeMillis() {
@@ -743,16 +742,7 @@
     }
 
     public void clearBuffer() {
-        // OpenGL seems having a bug causing us not being able to reset the
-        // scissor box in "setSize()". We have to do it in the second
-        // onDrawFrame().
-        if (mClipRetryCount > 0) {
-            --mClipRetryCount;
-            Rect clip = mClipRect;
-            mGL.glScissor(clip.left, clip.top, clip.width(), clip.height());
-        }
-
-        mGL.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
+        mGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
     }
 
     private void setTextureCoords(RectF source) {
diff --git a/new3d/src/com/android/gallery3d/ui/GLRootView.java b/new3d/src/com/android/gallery3d/ui/GLRootView.java
index e387f30..2a6b6d7 100644
--- a/new3d/src/com/android/gallery3d/ui/GLRootView.java
+++ b/new3d/src/com/android/gallery3d/ui/GLRootView.java
@@ -18,6 +18,7 @@
 
 import android.app.Activity;
 import android.content.Context;
+import android.graphics.Rect;
 import android.opengl.GLSurfaceView;
 import android.os.SystemClock;
 import android.util.AttributeSet;
@@ -62,6 +63,9 @@
     private int mFlags = FLAG_NEED_LAYOUT;
     private volatile boolean mRenderRequested = false;
 
+    private Rect mClipRect = new Rect();
+    private int mClipRetryCount = 0;
+
     private final GalleryEGLConfigChooser mEglConfigChooser =
             new GalleryEGLConfigChooser();
 
@@ -187,6 +191,9 @@
         Util.Assert(mGL == gl);
 
         mCanvas.setSize(width, height);
+
+        mClipRect.set(0, 0, width, height);
+        mClipRetryCount = 2;
     }
 
     public synchronized void onDrawFrame(GL10 gl) {
@@ -209,7 +216,16 @@
         mRenderRequested = false;
 
         if ((mFlags & FLAG_NEED_LAYOUT) != 0) layoutContentPane();
-        mCanvas.clearBuffer();
+
+        // OpenGL seems having a bug causing us not being able to reset the
+        // scissor box in "onSurfaceChanged()". We have to do it in the second
+        // onDrawFrame().
+        if (mClipRetryCount > 0) {
+            --mClipRetryCount;
+            Rect clip = mClipRect;
+            gl.glScissor(clip.left, clip.top, clip.width(), clip.height());
+        }
+
         mCanvas.setCurrentAnimationTimeMillis(SystemClock.uptimeMillis());
         if (mContentView != null) {
            mContentView.render(mCanvas);
diff --git a/new3d/src/com/android/gallery3d/ui/PhotoView.java b/new3d/src/com/android/gallery3d/ui/PhotoView.java
index 49068be..df3ae8c 100644
--- a/new3d/src/com/android/gallery3d/ui/PhotoView.java
+++ b/new3d/src/com/android/gallery3d/ui/PhotoView.java
@@ -139,6 +139,10 @@
             }
             return list.toArray(new Bitmap[list.size()]);
         }
+    }
 
+    @Override
+    protected void renderBackground(GLCanvas view) {
+        view.clearBuffer();
     }
 }
diff --git a/new3d/src/com/android/gallery3d/ui/PopupWindow.java b/new3d/src/com/android/gallery3d/ui/PopupWindow.java
index 784d046..fa1f9fc 100644
--- a/new3d/src/com/android/gallery3d/ui/PopupWindow.java
+++ b/new3d/src/com/android/gallery3d/ui/PopupWindow.java
@@ -124,6 +124,7 @@
         GL11 gl = canvas.getGLInstance();
         if (mAnchor != null) {
             gl.glEnable(GL11.GL_STENCIL_TEST);
+            gl.glClear(GL11.GL_STENCIL_BUFFER_BIT);
             gl.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_REPLACE);
             gl.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
             mAnchor.draw(canvas, aXoffset, aYoffset);
diff --git a/new3d/src/com/android/gallery3d/ui/StringTexture.java b/new3d/src/com/android/gallery3d/ui/StringTexture.java
index 62d84cf..3a0696c 100644
--- a/new3d/src/com/android/gallery3d/ui/StringTexture.java
+++ b/new3d/src/com/android/gallery3d/ui/StringTexture.java
@@ -28,7 +28,7 @@
     private final Paint mPaint;
     private final FontMetricsInt mMetrics;
 
-    public StringTexture(String text, Paint paint,
+    private StringTexture(String text, Paint paint,
             FontMetricsInt metrics, int width, int height) {
         super(width, height);
         mText = text;
@@ -36,8 +36,7 @@
         mMetrics = metrics;
     }
 
-
-    public static StringTexture newInstance(String text, Paint paint) {
+    private static StringTexture newInstance(String text, Paint paint) {
         FontMetricsInt metrics = paint.getFontMetricsInt();
         int width = (int) (.5f + paint.measureText(text)) + DEFAULT_PADDING * 2;
         int height = metrics.bottom - metrics.top + DEFAULT_PADDING * 2;
diff --git a/new3d/src/com/android/gallery3d/ui/UploadedTexture.java b/new3d/src/com/android/gallery3d/ui/UploadedTexture.java
index d0073a7..3178163 100644
--- a/new3d/src/com/android/gallery3d/ui/UploadedTexture.java
+++ b/new3d/src/com/android/gallery3d/ui/UploadedTexture.java
@@ -119,8 +119,7 @@
                 int widthExt = Util.nextPowerOf2(width);
                 int heightExt = Util.nextPowerOf2(height);
                 int format = GLUtils.getInternalFormat(bitmap);
-                mOpaque =
-                        (format == GL11.GL_RGB || format == GL11.GL_LUMINANCE);
+                mOpaque = !bitmap.hasAlpha();
                 int type = GLUtils.getType(bitmap);
 
                 mTextureWidth = widthExt;
@@ -158,7 +157,7 @@
     }
 
     public boolean isOpaque() {
-        return mOpaque ;
+        return mOpaque;
     }
 
     public void recycle() {
diff --git a/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java b/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
index 46fae89..5b950ab 100644
--- a/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
+++ b/new3d/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
@@ -38,7 +38,7 @@
             GLCanvas canvas = new GLCanvasImp(this);
             assertEquals(0, mGLClearCalled);
             canvas.clearBuffer();
-            assertTrue((mGLClearMask & GL10.GL_COLOR_BUFFER_BIT) != 0);
+            assertEquals(GL10.GL_COLOR_BUFFER_BIT, mGLClearMask);
             assertEquals(1, mGLClearCalled);
         }
     }
@@ -546,6 +546,7 @@
             assertEquals(42, mGLBindTextureId);
             assertEquals(GL_REPLACE, getTexEnvi(GL_TEXTURE_ENV_MODE));
             assertPremultipliedBlending(this);
+            assertFalse(mGLStencilEnabled);
 
             // Draw an opaque texture
             canvas.drawTexture(texture_o, 100, 200, 300, 400);
@@ -697,6 +698,7 @@
             assertEquals(GL_SRC_ALPHA, getTexEnvi(GL_OPERAND2_ALPHA));
             assertTrue(mTexture2DEnabled0);
             assertFalse(mTexture2DEnabled1);
+            assertFalse(mGLBlendEnabled);
 
             // The test is currently broken, waiting for the fix
             canvas.setAlpha(0.3f);
diff --git a/new3d/tests/src/com/android/gallery3d/ui/GLMock.java b/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
index c15c500..b92787e 100644
--- a/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
+++ b/new3d/tests/src/com/android/gallery3d/ui/GLMock.java
@@ -27,6 +27,7 @@
     int mGLColor;
     // glEnable, glDisable
     boolean mGLBlendEnabled;
+    boolean mGLStencilEnabled;
     // glEnableClientState
     boolean mGLVertexArrayEnabled;
     // glVertexPointer
@@ -73,6 +74,8 @@
     public void glEnable(int cap) {
         if (cap == GL11.GL_BLEND) {
             mGLBlendEnabled = true;
+        } else if (cap == GL11.GL_STENCIL_TEST) {
+            mGLStencilEnabled = true;
         }
     }
 
@@ -80,6 +83,8 @@
     public void glDisable(int cap) {
         if (cap == GL11.GL_BLEND) {
             mGLBlendEnabled = false;
+        } else if (cap == GL11.GL_STENCIL_TEST) {
+            mGLStencilEnabled = false;
         }
     }