OSDN Git Service

Reduce memory allocation and make small improvements.
authorChih-Chung Chang <chihchung@google.com>
Fri, 17 Feb 2012 22:17:18 +0000 (06:17 +0800)
committerChih-Chung Chang <chihchung@google.com>
Tue, 21 Feb 2012 18:26:45 +0000 (02:26 +0800)
Change-Id: Iac3f302454119de6363cd5cfb158619e739b0536

gallerycommon/src/com/android/gallery3d/common/Utils.java
src/com/android/gallery3d/app/AlbumPage.java
src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java
src/com/android/gallery3d/ui/AlbumSlidingWindow.java
src/com/android/gallery3d/ui/DialogDetailsView.java
src/com/android/gallery3d/ui/GLCanvas.java
src/com/android/gallery3d/ui/GLCanvasImpl.java
tests/src/com/android/gallery3d/ui/GLCanvasStub.java

index ea289a6..6996015 100644 (file)
@@ -54,12 +54,14 @@ public class Utils {
         }
     }
 
-    // Throws AssertionError if the input is false.
-    public static void assertTrue(boolean cond, String message, Object ... args) {
-        if (!cond) {
-            throw new AssertionError(
-                    args.length == 0 ? message : String.format(message, args));
-        }
+    // Throws AssertionError with the message. We had a method having the form
+    //   assertTrue(boolean cond, String message, Object ... args);
+    // However a call to that method will cause memory allocation even if the
+    // condition is false (due to autoboxing generated by "Object ... args"),
+    // so we don't use that anymore.
+    public static void fail(String message, Object ... args) {
+        throw new AssertionError(
+                args.length == 0 ? message : String.format(message, args));
     }
 
     // Throws NullPointerException if the input is null.
index 22b1899..99d86f5 100644 (file)
@@ -406,8 +406,9 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
     private void initializeData(Bundle data) {
         mMediaSetPath = Path.fromString(data.getString(KEY_MEDIA_PATH));
         mMediaSet = mActivity.getDataManager().getMediaSet(mMediaSetPath);
-        Utils.assertTrue(mMediaSet != null,
-                "MediaSet is null. Path = %s", mMediaSetPath);
+        if (mMediaSet == null) {
+            Utils.fail("MediaSet is null. Path = %s", mMediaSetPath);
+        }
         mSelectionManager.setSourceMediaSet(mMediaSet);
         mAlbumDataAdapter = new AlbumDataAdapter(mActivity, mMediaSet);
         mAlbumDataAdapter.setLoadingListener(new MyLoadingListener());
index 3991a93..b02f19e 100644 (file)
@@ -110,9 +110,10 @@ public class AlbumSetSlidingWindow implements AlbumSetView.ModelListener {
     }
 
     public AlbumSetItem get(int slotIndex) {
-        Utils.assertTrue(isActiveSlot(slotIndex),
-                "invalid slot: %s outsides (%s, %s)",
-                slotIndex, mActiveStart, mActiveEnd);
+        if (!isActiveSlot(slotIndex)) {
+            Utils.fail("invalid slot: %s outsides (%s, %s)",
+                    slotIndex, mActiveStart, mActiveEnd);
+        }
         return mData[slotIndex % mData.length];
     }
 
@@ -156,10 +157,10 @@ public class AlbumSetSlidingWindow implements AlbumSetView.ModelListener {
     }
 
     public void setActiveWindow(int start, int end) {
-        Utils.assertTrue(
-                start <= end && end - start <= mData.length && end <= mSize,
-                "start = %s, end = %s, length = %s, size = %s",
-                start, end, mData.length, mSize);
+        if (!(start <= end && end - start <= mData.length && end <= mSize)) {
+            Utils.fail("start = %s, end = %s, length = %s, size = %s",
+                    start, end, mData.length, mSize);
+        }
 
         AlbumSetItem data[] = mData;
 
index 6947d7f..d64c1f7 100644 (file)
@@ -117,9 +117,10 @@ public class AlbumSlidingWindow implements AlbumView.ModelListener {
     }
 
     public DisplayItem get(int slotIndex) {
-        Utils.assertTrue(isActiveSlot(slotIndex),
-                "invalid slot: %s outsides (%s, %s)",
-                slotIndex, mActiveStart, mActiveEnd);
+        if (!isActiveSlot(slotIndex)) {
+            Utils.fail("invalid slot: %s outsides (%s, %s)",
+                    slotIndex, mActiveStart, mActiveEnd);
+        }
         return mData[slotIndex % mData.length];
     }
 
@@ -170,9 +171,9 @@ public class AlbumSlidingWindow implements AlbumView.ModelListener {
     }
 
     public void setActiveWindow(int start, int end) {
-        Utils.assertTrue(start <= end
-                && end - start <= mData.length && end <= mSize,
-                "%s, %s, %s, %s", start, end, mData.length, mSize);
+        if (!(start <= end && end - start <= mData.length && end <= mSize)) {
+            Utils.fail("%s, %s, %s, %s", start, end, mData.length, mSize);
+        }
         DisplayItem data[] = mData;
 
         mActiveStart = start;
index c037f2a..580a40e 100644 (file)
@@ -173,8 +173,10 @@ public class DialogDetailsView implements DetailsViewContainer {
                     default: {
                         Object valueObj = detail.getValue();
                         // This shouldn't happen, log its key to help us diagnose the problem.
-                        Utils.assertTrue(valueObj != null, "%s's value is Null",
-                                DetailsHelper.getDetailsName(context, detail.getKey()));
+                        if (valueObj == null) {
+                            Utils.fail("%s's value is Null",
+                                    DetailsHelper.getDetailsName(context, detail.getKey()));
+                        }
                         value = valueObj.toString();
                     }
                 }
index eb78edd..52ce615 100644 (file)
@@ -60,10 +60,10 @@ public interface GLCanvas {
 
     // Pushes the configuration state (matrix, alpha, and clip) onto
     // a private stack.
-    public int save();
+    public void save();
 
     // Same as save(), but only save those specified in saveFlags.
-    public int save(int saveFlags);
+    public void save(int saveFlags);
 
     public static final int SAVE_FLAG_ALL = 0xFFFFFFFF;
     public static final int SAVE_FLAG_CLIP = 0x01;
index 85bf771..8775f08 100644 (file)
@@ -27,7 +27,7 @@ import android.opengl.Matrix;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
-import java.util.Stack;
+import java.util.ArrayList;
 import javax.microedition.khronos.opengles.GL10;
 import javax.microedition.khronos.opengles.GL11;
 import javax.microedition.khronos.opengles.GL11Ext;
@@ -63,8 +63,8 @@ public class GLCanvasImpl implements GLCanvas {
 
     private float mAlpha;
     private final Rect mClipRect = new Rect();
-    private final Stack<ConfigState> mRestoreStack =
-            new Stack<ConfigState>();
+    private final ArrayList<ConfigState> mRestoreStack =
+            new ArrayList<ConfigState>();
     private ConfigState mRecycledRestoreAction;
 
     private final RectF mDrawTextureSourceRect = new RectF();
@@ -229,6 +229,7 @@ public class GLCanvasImpl implements GLCanvas {
     }
 
     public void rotate(float angle, float x, float y, float z) {
+        if (angle == 0) return;
         float[] temp = mTempMatrix;
         Matrix.setRotateM(temp, 0, angle, x, y, z);
         Matrix.multiplyMM(temp, 16, mMatrixValues, 0, temp, 0);
@@ -361,10 +362,10 @@ public class GLCanvasImpl implements GLCanvas {
             // draw the rect from bottom-left to top-right
             float points[] = mapPoints(
                     mMatrixValues, x, y + height, x + width, y);
-            x = Math.round(points[0]);
-            y = Math.round(points[1]);
-            width = Math.round(points[2]) - x;
-            height = Math.round(points[3]) - y;
+            x = (int) (points[0] + 0.5f);
+            y = (int) (points[1] + 0.5f);
+            width = (int) (points[2] + 0.5f) - x;
+            height = (int) (points[3] + 0.5f) - y;
             if (width > 0 && height > 0) {
                 ((GL11Ext) mGL).glDrawTexiOES(x, y, 0, width, height);
                 mCountTextureOES++;
@@ -827,11 +828,11 @@ public class GLCanvasImpl implements GLCanvas {
         }
     }
 
-    public int save() {
-        return save(SAVE_FLAG_ALL);
+    public void save() {
+        save(SAVE_FLAG_ALL);
     }
 
-    public int save(int saveFlags) {
+    public void save(int saveFlags) {
         ConfigState config = obtainRestoreConfig();
 
         if ((saveFlags & SAVE_FLAG_ALPHA) != 0) {
@@ -852,13 +853,12 @@ public class GLCanvasImpl implements GLCanvas {
             config.mMatrix[0] = Float.NEGATIVE_INFINITY;
         }
 
-        mRestoreStack.push(config);
-        return mRestoreStack.size() - 1;
+        mRestoreStack.add(config);
     }
 
     public void restore() {
         if (mRestoreStack.isEmpty()) throw new IllegalStateException();
-        ConfigState config = mRestoreStack.pop();
+        ConfigState config = mRestoreStack.remove(mRestoreStack.size() - 1);
         config.restore(this);
         freeRestoreConfig(config);
     }
index 17d520d..41de108 100644 (file)
@@ -39,10 +39,10 @@ public class GLCanvasStub implements GLCanvas {
     public boolean clipRect(int left, int top, int right, int bottom) {
         throw new UnsupportedOperationException();
     }
-    public int save() {
+    public void save() {
         throw new UnsupportedOperationException();
     }
-    public int save(int saveFlags) {
+    public void save(int saveFlags) {
         throw new UnsupportedOperationException();
     }
     public void setBlendEnabled(boolean enabled) {}