OSDN Git Service

Improve the Texture by using glTexSubImage to upload the image content.
authorOwen Lin <owenlin@google.com>
Thu, 29 Apr 2010 06:20:25 +0000 (14:20 +0800)
committerOwen Lin <owenlin@google.com>
Thu, 29 Apr 2010 06:20:25 +0000 (14:20 +0800)
So that we can prevent create another Bitmap just to satisfied the limit that
the width and height must be power of 2.

Change-Id: Ie0cdb1e944fb2bf928464955851fe62d00394d70

src/com/android/camera/ui/CanvasTexture.java
src/com/android/camera/ui/ResourceTexture.java
src/com/android/camera/ui/Texture.java

index f1ac85a..fa0b76e 100644 (file)
@@ -2,6 +2,7 @@ package com.android.camera.ui;
 
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
+import android.graphics.Bitmap.Config;
 
 /** Using a canvas to draw the texture */
 public abstract class CanvasTexture extends Texture {
@@ -13,7 +14,7 @@ public abstract class CanvasTexture extends Texture {
 
     @Override
     protected Bitmap getBitmap() {
-        Bitmap bitmap = generateGLCompatibleBitmap(mWidth, mHeight);
+        Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
         mCanvas = new Canvas(bitmap);
         onDraw(mCanvas, bitmap);
         return bitmap;
index 0d19d2a..e63a2ef 100644 (file)
@@ -1,12 +1,10 @@
 package com.android.camera.ui;
 
+import com.android.camera.Util;
+
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
-import android.graphics.Canvas;
-import android.graphics.Matrix;
-
-import com.android.camera.Util;
 
 public class ResourceTexture extends Texture {
 
@@ -22,17 +20,11 @@ public class ResourceTexture extends Texture {
     @Override
     protected Bitmap getBitmap() {
         if (mBitmap != null) return mBitmap;
-        mBitmap = BitmapFactory.decodeResource(mContext.getResources(), mResId);
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+        mBitmap = BitmapFactory.decodeResource(
+                mContext.getResources(), mResId, options);
         setSize(mBitmap.getWidth(), mBitmap.getHeight());
-
-        if (Util.isPowerOf2(mWidth) && Util.isPowerOf2(mHeight)) return mBitmap;
-
-        Bitmap oldBitmap = mBitmap;
-        mBitmap = generateGLCompatibleBitmap(mWidth, mHeight);
-        Canvas canvas = new Canvas(mBitmap);
-        canvas.drawBitmap(oldBitmap, new Matrix(), null);
-        oldBitmap.recycle();
-
         return mBitmap;
     }
 
index a384d3a..0d4b6a1 100644 (file)
@@ -1,11 +1,10 @@
 package com.android.camera.ui;
 
+import com.android.camera.Util;
+
 import android.graphics.Bitmap;
-import android.graphics.Bitmap.Config;
 import android.opengl.GLUtils;
 
-import com.android.camera.Util;
-
 import javax.microedition.khronos.opengles.GL11;
 import javax.microedition.khronos.opengles.GL11Ext;
 
@@ -96,7 +95,19 @@ public abstract class Texture {
                         GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
                 gl.glTexParameterf(GL11.GL_TEXTURE_2D,
                         GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
-                GLUtils.texImage2D(GL11.GL_TEXTURE_2D, 0, bitmap, 0);
+
+                int width = bitmap.getWidth();
+                int height = bitmap.getHeight();
+                int widthExt = Util.nextPowerOf2(width);
+                int heightExt = Util.nextPowerOf2(height);
+                int format = GLUtils.getInternalFormat(bitmap);
+                int type = GLUtils.getType(bitmap);
+                mTexCoordWidth = (float) width / widthExt;
+                mTexCoordHeight = (float) height / heightExt;
+                gl.glTexImage2D(GL11.GL_TEXTURE_2D, 0, format,
+                        widthExt, heightExt, 0, format, type, null);
+                GLUtils.texSubImage2D(
+                        GL11.GL_TEXTURE_2D, 0, 0, 0, bitmap, format, type);
             } finally {
                 freeBitmap(bitmap);
             }
@@ -156,12 +167,4 @@ public abstract class Texture {
         coord[offset++] = w;
         coord[offset] = h;
     }
-
-    protected Bitmap generateGLCompatibleBitmap(int width, int height) {
-        int newWidth = Util.nextPowerOf2(width);
-        int newHeight = Util.nextPowerOf2(height);
-        mTexCoordWidth = (float) width / newWidth;
-        mTexCoordHeight = (float) height / newHeight;
-        return Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
-    }
 }