OSDN Git Service

Draw less to improve performance.
authorOwen Lin <owenlin@google.com>
Thu, 5 Apr 2012 04:45:54 +0000 (12:45 +0800)
committerOwen Lin <owenlin@google.com>
Mon, 9 Apr 2012 09:16:53 +0000 (17:16 +0800)
Also adjust the upload order to upload nearby textures first.

Change-Id: I2d6a8807a14b4602882dd2e5c03030c356f49e47

res/drawable/dark_strip.9.png [deleted file]
src/com/android/gallery3d/ui/AlbumLabelMaker.java
src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java
src/com/android/gallery3d/ui/AlbumSetView.java
src/com/android/gallery3d/ui/AlbumSlidingWindow.java
src/com/android/gallery3d/ui/AlbumView.java

diff --git a/res/drawable/dark_strip.9.png b/res/drawable/dark_strip.9.png
deleted file mode 100644 (file)
index dba0cae..0000000
Binary files a/res/drawable/dark_strip.9.png and /dev/null differ
index 4eac3cb..317fe52 100644 (file)
@@ -24,6 +24,10 @@ public class AlbumLabelMaker {
     private static final int FONT_COLOR_TITLE = Color.WHITE;
     private static final int FONT_COLOR_COUNT = 0x80FFFFFF;  // 50% white
 
+    // We keep a border around the album label to prevent aliasing
+    private static final int BORDER_SIZE = 1;
+    private static final int BACKGROUND_COLOR = 0x60000000; // 36% Dark
+
     private final AlbumSetView.LabelSpec mSpec;
     private final TextPaint mTitlePaint;
     private final TextPaint mCountPaint;
@@ -49,6 +53,10 @@ public class AlbumLabelMaker {
         mMtpIcon = new LazyLoadedBitmap(R.drawable.frame_overlay_gallery_ptp);
     }
 
+    public static int getBorderSize() {
+        return BORDER_SIZE;
+    }
+
     private Bitmap getOverlayAlbumIcon(int sourceType) {
         switch (sourceType) {
             case DataSourceType.TYPE_CAMERA:
@@ -97,7 +105,9 @@ public class AlbumLabelMaker {
     public synchronized void setLabelWidth(int width) {
         if (mLabelWidth == width) return;
         mLabelWidth = width;
-        mBitmapPool = new BitmapPool(mLabelWidth, mSpec.labelBackgroundHeight);
+        int borders = 2 * BORDER_SIZE;
+        mBitmapPool = new BitmapPool(
+                width + borders, mSpec.labelBackgroundHeight + borders);
     }
 
     public ThreadPool.Job<Bitmap> requestLabel(
@@ -146,23 +156,28 @@ public class AlbumLabelMaker {
                     : String.valueOf(album.getTotalMediaItemCount());
             Bitmap icon = getOverlayAlbumIcon(mSourceType);
 
-            Bitmap bitmap = null;
-            Canvas canvas;
+            Bitmap bitmap;
             int labelWidth;
 
             synchronized (this) {
                 labelWidth = mLabelWidth;
                 bitmap = mBitmapPool.getBitmap();
             }
+
             if (bitmap == null) {
-                bitmap = Bitmap.createBitmap(labelWidth,
-                        s.labelBackgroundHeight, Config.ARGB_8888);
-                canvas = new Canvas(bitmap);
-            } else {
-                canvas = new Canvas(bitmap);
-                canvas.drawColor(0, PorterDuff.Mode.SRC);
+                int borders = 2 * BORDER_SIZE;
+                bitmap = Bitmap.createBitmap(labelWidth + borders,
+                        s.labelBackgroundHeight + borders, Config.ARGB_8888);
             }
 
+            Canvas canvas = new Canvas(bitmap);
+            canvas.clipRect(BORDER_SIZE, BORDER_SIZE,
+                    bitmap.getWidth() - BORDER_SIZE,
+                    bitmap.getHeight() - BORDER_SIZE);
+            canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC);
+
+            canvas.translate(BORDER_SIZE, BORDER_SIZE);
+
             // draw title
             if (jc.isCancelled()) return null;
             int x = s.leftMargin;
index 606ab59..784093c 100644 (file)
@@ -276,23 +276,38 @@ public class AlbumSetSlidingWindow implements AlbumSetView.ModelListener {
         return loader.isRequestInProgress();
     }
 
-    private void queueTextureForUpload(boolean isActive, Texture texture) {
-        if ((texture == null) || !(texture instanceof BitmapTexture)) return;
-        if (isActive) {
-            mTextureUploader.addFgTexture((BitmapTexture) texture);
-        } else {
-            mTextureUploader.addBgTexture((BitmapTexture) texture);
+    private void uploadBackgroundTextureInSlot(int index) {
+        if (index < mContentStart || index >= mContentEnd) return;
+        AlbumSetEntry entry = mData[index % mData.length];
+        if (entry.content instanceof BitmapTexture) {
+            mTextureUploader.addBgTexture((BitmapTexture) entry.content);
+        }
+        if (entry.label instanceof BitmapTexture) {
+            mTextureUploader.addBgTexture((BitmapTexture) entry.label);
         }
     }
 
     private void updateTextureUploadQueue() {
         if (!mIsActive) return;
         mTextureUploader.clear();
-        for (int i = mContentStart, n = mContentEnd; i < n; ++i) {
+
+        // Upload foreground texture
+        for (int i = mActiveStart, n = mActiveEnd; i < n; ++i) {
             AlbumSetEntry entry = mData[i % mData.length];
-            boolean isActive = isActiveSlot(i);
-            queueTextureForUpload(isActive, entry.label);
-            queueTextureForUpload(isActive, entry.content);
+            if (entry.content instanceof BitmapTexture) {
+                mTextureUploader.addFgTexture((BitmapTexture) entry.content);
+            }
+            if (entry.label instanceof BitmapTexture) {
+                mTextureUploader.addFgTexture((BitmapTexture) entry.label);
+            }
+        }
+
+        // add background textures
+        int range = Math.max(
+                (mContentEnd - mActiveEnd), (mActiveStart - mContentStart));
+        for (int i = 0; i < range; ++i) {
+            uploadBackgroundTextureInSlot(mActiveEnd + i);
+            uploadBackgroundTextureInSlot(mActiveStart - i - 1);
         }
     }
 
index 440487e..03b9230 100644 (file)
@@ -18,7 +18,6 @@ package com.android.gallery3d.ui;
 
 import android.content.Context;
 
-import com.android.gallery3d.R;
 import com.android.gallery3d.app.GalleryActivity;
 import com.android.gallery3d.data.MediaItem;
 import com.android.gallery3d.data.MediaObject;
@@ -30,7 +29,7 @@ import com.android.gallery3d.ui.AlbumSetSlidingWindow.AlbumSetEntry;
 public class AlbumSetView extends AbstractSlotRenderer {
     @SuppressWarnings("unused")
     private static final String TAG = "AlbumSetView";
-    private static final int CACHE_SIZE = 64;
+    private static final int CACHE_SIZE = 96;
     private static final int PLACEHOLDER_COLOR = 0xFF222222;
 
     private final ColorTexture mWaitLoadingTexture;
@@ -40,7 +39,6 @@ public class AlbumSetView extends AbstractSlotRenderer {
 
     protected AlbumSetSlidingWindow mDataWindow;
     private SlotView mSlotView;
-    private NinePatchTexture mDarkStrip;
 
     private int mPressedIndex = -1;
     private Path mHighlightItemPath = null;
@@ -81,7 +79,6 @@ public class AlbumSetView extends AbstractSlotRenderer {
         mWaitLoadingTexture.setSize(1, 1);
 
         Context context = activity.getAndroidContext();
-        mDarkStrip = new NinePatchTexture(context, R.drawable.dark_strip);
     }
 
     public void setPressedIndex(int index) {
@@ -180,9 +177,9 @@ public class AlbumSetView extends AbstractSlotRenderer {
             content = mDataWindow.getLoadingTexture();
         }
         if (content != null) {
-            int h = mLabelSpec.labelBackgroundHeight;
-            drawFrame(canvas, mDarkStrip, 0, height - h, width, h);
-            content.draw(canvas, 0, height - h, width, h);
+            int b = AlbumLabelMaker.getBorderSize();
+            int h = content.getHeight();
+            content.draw(canvas, -b, height - h + b, width + b + b, h);
         }
         return 0;
     }
index af6dc46..4fbbe19 100644 (file)
@@ -20,7 +20,6 @@ import android.graphics.Bitmap;
 import android.os.Message;
 
 import com.android.gallery3d.app.GalleryActivity;
-import com.android.gallery3d.common.BitmapUtils;
 import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.data.MediaItem;
 import com.android.gallery3d.data.Path;
@@ -157,26 +156,37 @@ public class AlbumSlidingWindow implements AlbumView.ModelListener {
                 0, Math.max(0, mSize - data.length));
         int contentEnd = Math.min(contentStart + data.length, mSize);
         setContentWindow(contentStart, contentEnd);
-        updateUploadedTextures();
+        updateTextureUploadQueue();
         if (mIsActive) updateAllImageRequests();
     }
 
-    private void uploadTexture(boolean isActive, Texture texture) {
-        if ((texture == null) || !(texture instanceof BitmapTexture)) return;
-        if (isActive) {
-            mTextureUploader.addFgTexture((BitmapTexture) texture);
-        } else {
-            mTextureUploader.addBgTexture((BitmapTexture) texture);
+    private void uploadBgTextureInSlot(int index) {
+        if (index < mContentEnd && index >= mContentStart) {
+            AlbumEntry entry = mData[index % mData.length];
+            if (entry.content instanceof BitmapTexture) {
+                mTextureUploader.addBgTexture((BitmapTexture) entry.content);
+            }
         }
     }
 
-    private void updateUploadedTextures() {
+    private void updateTextureUploadQueue() {
         if (!mIsActive) return;
         mTextureUploader.clear();
-        for (int i = mContentStart, n = mContentEnd; i < n; ++i) {
+
+        // add foreground textures
+        for (int i = mActiveStart, n = mActiveEnd; i < n; ++i) {
             AlbumEntry entry = mData[i % mData.length];
-            boolean isActive = isActiveSlot(i);
-            uploadTexture(isActive, entry.content);
+            if (entry.content instanceof BitmapTexture) {
+                mTextureUploader.addFgTexture((BitmapTexture) entry.content);
+            }
+        }
+
+        // add background textures
+        int range = Math.max(
+                (mContentEnd - mActiveEnd), (mActiveStart - mContentStart));
+        for (int i = 0; i < range; ++i) {
+            uploadBgTextureInSlot(mActiveEnd + i);
+            uploadBgTextureInSlot(mActiveStart - i - 1);
         }
     }
 
index 7402280..04c2914 100644 (file)
@@ -28,7 +28,7 @@ public class AlbumView extends AbstractSlotRenderer {
 
     @SuppressWarnings("unused")
     private static final String TAG = "AlbumView";
-    private static final int CACHE_SIZE = 64;
+    private static final int CACHE_SIZE = 96;
 
     private AlbumSlidingWindow mDataWindow;
     private final GalleryActivity mActivity;