Move the texture coordinate 0.5 pixel inner so that GL won't get garbage
data.

So that GL won't get garbage data when do sampling.

Change-Id: I58a88c2fb1166e0c3991edfd320c69633c89d102
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java
index f59e020..7f928bd 100644
--- a/src/com/android/camera/Camera.java
+++ b/src/com/android/camera/Camera.java
@@ -45,6 +45,7 @@
 import android.os.Debug;
 import android.os.Environment;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Message;
 import android.os.MessageQueue;
 import android.os.SystemClock;
@@ -370,7 +371,7 @@
     }
 
     private void addIdleHandler() {
-        MessageQueue queue = getMainLooper().myQueue();
+        MessageQueue queue = Looper.myQueue();
         queue.addIdleHandler(new MessageQueue.IdleHandler() {
             public boolean queueIdle() {
                 ImageManager.ensureOSXCompatibleFolder();
diff --git a/src/com/android/camera/ui/GLRootView.java b/src/com/android/camera/ui/GLRootView.java
index 30adccb..dc10be4 100644
--- a/src/com/android/camera/ui/GLRootView.java
+++ b/src/com/android/camera/ui/GLRootView.java
@@ -525,8 +525,7 @@
         }
 
         texture.setSize(width, height);
-        texture.setTexCoordSize(
-                (float) width / newWidth, (float) height / newHeight);
+        texture.setTextureSize(newWidth, newHeight);
     }
 
     public synchronized void queueEventOrThrowException(Runnable runnable) {
diff --git a/src/com/android/camera/ui/Texture.java b/src/com/android/camera/ui/Texture.java
index 0d4b6a1..e1d6e39 100644
--- a/src/com/android/camera/ui/Texture.java
+++ b/src/com/android/camera/ui/Texture.java
@@ -26,8 +26,8 @@
     protected int mWidth = UNSPECIFIED;
     protected int mHeight = UNSPECIFIED;
 
-    private float mTexCoordWidth = 1.0f;
-    private float mTexCoordHeight = 1.0f;
+    private int mTextureWidth;
+    private int mTextureHeight;
 
     protected Texture(GL11 gl, int id, int state) {
         mGL = gl;
@@ -44,9 +44,14 @@
         mHeight = height;
     }
 
-    protected void setTexCoordSize(float width, float height) {
-        mTexCoordWidth = width;
-        mTexCoordHeight = height;
+    /**
+     * Sets the size of the texture. Due to the limit of OpenGL, the texture
+     * size must be of power of 2, the size of the content may not be the size
+     * of the texture.
+     */
+    protected void setTextureSize(int width, int height) {
+        mTextureWidth = width;
+        mTextureHeight = height;
     }
 
     public int getId() {
@@ -102,8 +107,9 @@
                 int heightExt = Util.nextPowerOf2(height);
                 int format = GLUtils.getInternalFormat(bitmap);
                 int type = GLUtils.getType(bitmap);
-                mTexCoordWidth = (float) width / widthExt;
-                mTexCoordHeight = (float) height / heightExt;
+
+                mTextureWidth = widthExt;
+                mTextureHeight = heightExt;
                 gl.glTexImage2D(GL11.GL_TEXTURE_2D, 0, format,
                         widthExt, heightExt, 0, format, type, null);
                 GLUtils.texSubImage2D(
@@ -155,16 +161,20 @@
     }
 
     public void getTextureCoords(float coord[], int offset) {
-        float w = mTexCoordWidth;
-        float h = mTexCoordHeight;
+        // Shrinks the texture coordinates inner by 0.5 pixel so that GL won't
+        // sample on garbage data.
+        float left = 0.5f / mTextureWidth;
+        float right = (mWidth - 0.5f) / mTextureWidth;
+        float top = 0.5f / mTextureHeight;
+        float bottom = (mHeight - 0.5f) / mTextureHeight;
 
-        coord[offset++] = 0;
-        coord[offset++] = 0;
-        coord[offset++] = w;
-        coord[offset++] = 0;
-        coord[offset++] = 0;
-        coord[offset++] = h;
-        coord[offset++] = w;
-        coord[offset] = h;
+        coord[offset++] = left;
+        coord[offset++] = top;
+        coord[offset++] = right;
+        coord[offset++] = top;
+        coord[offset++] = left;
+        coord[offset++] = bottom;
+        coord[offset++] = right;
+        coord[offset] = bottom;
     }
 }