Skip to content

Commit

Permalink
Make PlatformBitmapFactory a true singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
tyronen committed Jul 20, 2015
1 parent be644d6 commit 6e1fef3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,24 @@

package com.facebook.imagepipeline.core;

import javax.annotation.Nullable;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;

import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Supplier;
import com.facebook.common.memory.MemoryTrimmableRegistry;
import com.facebook.common.memory.NoOpMemoryTrimmableRegistry;
import com.facebook.common.util.ByteConstants;
import com.facebook.imagepipeline.animated.base.AnimatedImageResult;
import com.facebook.imagepipeline.animated.factory.AnimatedImageFactory;
import com.facebook.imagepipeline.animated.impl.AnimatedDrawableBackendImpl;
import com.facebook.imagepipeline.animated.util.AnimatedDrawableUtil;
import com.facebook.imagepipeline.bitmaps.GingerbreadBitmapFactory;
import com.facebook.imagepipeline.bitmaps.DalvikBitmapFactory;
import com.facebook.imagepipeline.bitmaps.ArtBitmapFactory;
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;
import com.facebook.imagepipeline.bitmaps.EmptyJpegGenerator;
import com.facebook.imagepipeline.cache.DefaultBitmapMemoryCacheParamsSupplier;
import com.facebook.imagepipeline.cache.CacheKeyFactory;
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory;
Expand All @@ -44,8 +37,6 @@
import com.facebook.imagepipeline.decoder.ProgressiveJpegConfig;
import com.facebook.imagepipeline.decoder.ImageDecoder;
import com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend;
import com.facebook.imagepipeline.animated.impl.AnimatedDrawableBackendProvider;
import com.facebook.imagepipeline.memory.PoolConfig;
import com.facebook.imagepipeline.memory.PoolFactory;
import com.facebook.imagepipeline.listener.RequestListener;
Expand All @@ -69,30 +60,32 @@
*/
public class ImagePipelineConfig {

// If a member here is marked @Nullable, it must be constructed by ImagePipelineFactory
// on demand if needed.

// There are a lot of parameters in this class. Please follow strict alphabetical order.
private final AnimatedDrawableUtil mAnimatedDrawableUtil;
private final AnimatedImageFactory mAnimatedImageFactory;
@Nullable private final AnimatedImageFactory mAnimatedImageFactory;
private final Supplier<MemoryCacheParams> mBitmapMemoryCacheParamsSupplier;
private final CacheKeyFactory mCacheKeyFactory;
private final Context mContext;
private final boolean mDownsampleEnabled;
private final Supplier<MemoryCacheParams> mEncodedMemoryCacheParamsSupplier;
private final ExecutorSupplier mExecutorSupplier;
private final ImageCacheStatsTracker mImageCacheStatsTracker;
private final ImageDecoder mImageDecoder;
@Nullable private final ImageDecoder mImageDecoder;
private final Supplier<Boolean> mIsPrefetchEnabledSupplier;
private final DiskCacheConfig mMainDiskCacheConfig;
private final MemoryTrimmableRegistry mMemoryTrimmableRegistry;
private final NetworkFetcher mNetworkFetcher;
private final PlatformBitmapFactory mPlatformBitmapFactory;
@Nullable private final PlatformBitmapFactory mPlatformBitmapFactory;
private final PoolFactory mPoolFactory;
private final ProgressiveJpegConfig mProgressiveJpegConfig;
private final Set<RequestListener> mRequestListeners;
private final boolean mResizeAndRotateEnabledForNetwork;
private final DiskCacheConfig mSmallImageDiskCacheConfig;

private ImagePipelineConfig(Builder builder) {
mAnimatedDrawableUtil = new AnimatedDrawableUtil();
mAnimatedImageFactory = builder.mAnimatedImageFactory;
mBitmapMemoryCacheParamsSupplier =
builder.mBitmapMemoryCacheParamsSupplier == null ?
new DefaultBitmapMemoryCacheParamsSupplier(
Expand All @@ -112,15 +105,16 @@ private ImagePipelineConfig(Builder builder) {
builder.mImageCacheStatsTracker == null ?
NoOpImageCacheStatsTracker.getInstance() :
builder.mImageCacheStatsTracker;
mImageDecoder = builder.mImageDecoder;
mIsPrefetchEnabledSupplier =
builder.mIsPrefetchEnabledSupplier == null ?
new Supplier<Boolean>() {
@Override
public Boolean get() {
return true;
}
} :
builder.mIsPrefetchEnabledSupplier;
builder.mIsPrefetchEnabledSupplier == null ?
new Supplier<Boolean>() {
@Override
public Boolean get() {
return true;
}
} :
builder.mIsPrefetchEnabledSupplier;
mMainDiskCacheConfig =
builder.mMainDiskCacheConfig == null ?
getDefaultMainDiskCacheConfig(builder.mContext) :
Expand All @@ -133,6 +127,7 @@ public Boolean get() {
builder.mNetworkFetcher == null ?
new HttpUrlConnectionNetworkFetcher() :
builder.mNetworkFetcher;
mPlatformBitmapFactory = builder.mPlatformBitmapFactory;
mPoolFactory =
builder.mPoolFactory == null ?
new PoolFactory(PoolConfig.newBuilder().build()) :
Expand All @@ -154,51 +149,9 @@ public Boolean get() {
// Below this comment can't be built in alphabetical order, because of dependencies

int decodeThreads = mPoolFactory.getFlexByteArrayPoolMaxNumThreads();

mExecutorSupplier =
builder.mExecutorSupplier == null ?
new DefaultExecutorSupplier(decodeThreads) : builder.mExecutorSupplier;

mPlatformBitmapFactory =
buildPlatformBitmapFactory(mPoolFactory, decodeThreads, mDownsampleEnabled);

AnimatedDrawableBackendProvider animatedDrawableBackendProvider =
new AnimatedDrawableBackendProvider() {
@Override
public AnimatedDrawableBackend get(AnimatedImageResult imageResult, Rect bounds) {
return new AnimatedDrawableBackendImpl(mAnimatedDrawableUtil, imageResult, bounds);
}
};
mAnimatedImageFactory = builder.mAnimatedImageFactory == null ?
new AnimatedImageFactory(animatedDrawableBackendProvider, mPlatformBitmapFactory) :
builder.mAnimatedImageFactory;

mImageDecoder =
builder.mImageDecoder == null ?
new ImageDecoder(mAnimatedImageFactory, mPlatformBitmapFactory) :
builder.mImageDecoder;

}

private static PlatformBitmapFactory buildPlatformBitmapFactory(
PoolFactory poolFactory,
int decodeThreads,
boolean downsampleEnabled) {
GingerbreadBitmapFactory factoryGingerbread =
Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ?
new GingerbreadBitmapFactory() : null;
DalvikBitmapFactory factoryICS = new DalvikBitmapFactory(
new EmptyJpegGenerator(poolFactory.getPooledByteBufferFactory()),
poolFactory.getFlexByteArrayPool(),
downsampleEnabled);
ArtBitmapFactory factoryLollipop =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ?
new ArtBitmapFactory(poolFactory.getBitmapPool(), decodeThreads, downsampleEnabled) :
null;
return new PlatformBitmapFactory(
factoryGingerbread,
factoryICS,
factoryLollipop);
}

private static DiskCacheConfig getDefaultMainDiskCacheConfig(final Context context) {
Expand All @@ -217,6 +170,11 @@ public File get() {
.build();
}

@Nullable
public AnimatedImageFactory getAnimatedImageFactory() {
return mAnimatedImageFactory;
}

public Supplier<MemoryCacheParams> getBitmapMemoryCacheParamsSupplier() {
return mBitmapMemoryCacheParamsSupplier;
}
Expand All @@ -241,6 +199,7 @@ public ImageCacheStatsTracker getImageCacheStatsTracker() {
return mImageCacheStatsTracker;
}

@Nullable
public ImageDecoder getImageDecoder() {
return mImageDecoder;
}
Expand All @@ -265,6 +224,11 @@ public boolean isDownsampleEnabled() {
return mDownsampleEnabled;
}

@Nullable
public PlatformBitmapFactory getPlatformBitmapFactory() {
return mPlatformBitmapFactory;
}

public PoolFactory getPoolFactory() {
return mPoolFactory;
}
Expand All @@ -285,10 +249,6 @@ public DiskCacheConfig getSmallImageDiskCacheConfig() {
return mSmallImageDiskCacheConfig;
}

public PlatformBitmapFactory getPlatformBitmapFactory() {
return mPlatformBitmapFactory;
}

public static Builder newBuilder(Context context) {
return new Builder(context);
}
Expand All @@ -308,6 +268,7 @@ public static class Builder {
private DiskCacheConfig mMainDiskCacheConfig;
private MemoryTrimmableRegistry mMemoryTrimmableRegistry;
private NetworkFetcher mNetworkFetcher;
private PlatformBitmapFactory mPlatformBitmapFactory;
private PoolFactory mPoolFactory;
private ProgressiveJpegConfig mProgressiveJpegConfig;
private Set<RequestListener> mRequestListeners;
Expand Down Expand Up @@ -383,6 +344,11 @@ public Builder setNetworkFetcher(NetworkFetcher networkFetcher) {
return this;
}

public Builder setPlatformBitmapFactory(PlatformBitmapFactory platformBitmapFactory) {
mPlatformBitmapFactory = platformBitmapFactory;
return this;
}

public Builder setPoolFactory(PoolFactory poolFactory) {
mPoolFactory = poolFactory;
return this;
Expand Down
Loading

0 comments on commit 6e1fef3

Please sign in to comment.