Skip to content

Commit

Permalink
Add api to set default decode format.
Browse files Browse the repository at this point in the history
Fixes #177.
  • Loading branch information
sjudd committed Oct 17, 2014
1 parent d88c2f7 commit 162c0ca
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.gifdecoder.GifHeader;
import com.bumptech.glide.gifdecoder.GifHeaderParser;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.tests.GlideShadowLooper;

Expand Down Expand Up @@ -54,7 +55,8 @@ public void setUp() {
decoderPool = mock(GifResourceDecoder.GifDecoderPool.class);
when(decoderPool.obtain(any(GifDecoder.BitmapProvider.class))).thenReturn(gifDecoder);

decoder = new GifResourceDecoder(Robolectric.application, bitmapPool, parserPool, decoderPool);
decoder = new GifResourceDecoder(Robolectric.application, bitmapPool, DecodeFormat.PREFER_RGB_565,
parserPool, decoderPool);
}

@Test
Expand Down Expand Up @@ -97,6 +99,23 @@ public void testReturnsParserToPoolWhenParserThrows() {
verify(parserPool).release(eq(parser));
}


@Test
public void testSetsPreferredConfigOnDecoderBeforeDecoding() {
when(gifHeader.getNumFrames()).thenReturn(1);
when(gifHeader.getStatus()).thenReturn(GifDecoder.STATUS_OK);
when(gifDecoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));

decoder = new GifResourceDecoder(Robolectric.application, mock(BitmapPool.class), DecodeFormat.ALWAYS_ARGB_8888,
parserPool, decoderPool);

decoder.decode(new ByteArrayInputStream(new byte[0]), 100, 100);

InOrder order = inOrder(gifDecoder);
order.verify(gifDecoder).setPreferredConfig(eq(Bitmap.Config.ARGB_8888));
order.verify(gifDecoder).getNextFrame();
}

@Test
public void testDecodesFirstFrameAndReturnsGifDecoderToPool() {
when(gifHeader.getNumFrames()).thenReturn(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ public class BitmapRequestBuilder<ModelType, TranscodeType>
private final BitmapPool bitmapPool;

private Downsampler downsampler = Downsampler.AT_LEAST;
private DecodeFormat decodeFormat = DecodeFormat.PREFER_RGB_565;
private DecodeFormat decodeFormat;
private ResourceDecoder<InputStream, Bitmap> imageDecoder;
private ResourceDecoder<ParcelFileDescriptor, Bitmap> videoDecoder;

BitmapRequestBuilder(LoadProvider<ModelType, ImageVideoWrapper, Bitmap, TranscodeType> loadProvider,
Class<TranscodeType> transcodeClass, GenericRequestBuilder<ModelType, ?, ?, ?> other) {
super(loadProvider, transcodeClass, other);
this.bitmapPool = other.glide.getBitmapPool();
this.decodeFormat = other.glide.getDecodeFormat();

imageDecoder = new StreamBitmapDecoder(bitmapPool);
videoDecoder = new FileDescriptorBitmapDecoder(bitmapPool);
imageDecoder = new StreamBitmapDecoder(bitmapPool, decodeFormat);
videoDecoder = new FileDescriptorBitmapDecoder(bitmapPool, decodeFormat);
}

/**
Expand Down
26 changes: 19 additions & 7 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.prefill.PreFillBitmapAttribute;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class Glide {
private final Engine engine;
private final BitmapPool bitmapPool;
private final MemoryCache memoryCache;
private final DecodeFormat decodeFormat;
private final ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
private final TranscoderRegistry transcoderRegistry = new TranscoderRegistry();
private final DataLoadProviderRegistry dataLoadProviderRegistry;
Expand Down Expand Up @@ -181,24 +183,30 @@ static void tearDown() {
glide = null;
}

Glide(Engine engine, MemoryCache memoryCache, BitmapPool bitmapPool, Context context) {
Glide(Engine engine, MemoryCache memoryCache, BitmapPool bitmapPool, Context context, DecodeFormat decodeFormat) {
this.engine = engine;
this.bitmapPool = bitmapPool;
this.memoryCache = memoryCache;
this.decodeFormat = decodeFormat;
mainHandler = new Handler(Looper.getMainLooper());
bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool);

dataLoadProviderRegistry = new DataLoadProviderRegistry();

dataLoadProviderRegistry.register(InputStream.class, Bitmap.class,
new StreamBitmapDataLoadProvider(bitmapPool));
dataLoadProviderRegistry.register(ParcelFileDescriptor.class, Bitmap.class,
new FileDescriptorBitmapDataLoadProvider(bitmapPool));
StreamBitmapDataLoadProvider streamBitmapLoadProvider =
new StreamBitmapDataLoadProvider(bitmapPool, decodeFormat);
dataLoadProviderRegistry.register(InputStream.class, Bitmap.class, streamBitmapLoadProvider);

ImageVideoDataLoadProvider imageVideoDataLoadProvider = new ImageVideoDataLoadProvider(bitmapPool);
FileDescriptorBitmapDataLoadProvider fileDescriptorLoadProvider =
new FileDescriptorBitmapDataLoadProvider(bitmapPool, decodeFormat);
dataLoadProviderRegistry.register(ParcelFileDescriptor.class, Bitmap.class, fileDescriptorLoadProvider);

ImageVideoDataLoadProvider imageVideoDataLoadProvider =
new ImageVideoDataLoadProvider(streamBitmapLoadProvider, fileDescriptorLoadProvider);
dataLoadProviderRegistry.register(ImageVideoWrapper.class, Bitmap.class, imageVideoDataLoadProvider);

GifDrawableLoadProvider gifDrawableLoadProvider = new GifDrawableLoadProvider(context, bitmapPool);
GifDrawableLoadProvider gifDrawableLoadProvider =
new GifDrawableLoadProvider(context, bitmapPool, decodeFormat);
dataLoadProviderRegistry.register(InputStream.class, GifDrawable.class, gifDrawableLoadProvider);

dataLoadProviderRegistry.register(ImageVideoWrapper.class, GifBitmapWrapper.class,
Expand Down Expand Up @@ -295,6 +303,10 @@ Handler getMainHandler() {
return mainHandler;
}

DecodeFormat getDecodeFormat() {
return decodeFormat;
}

private GenericLoaderFactory getLoaderFactory() {
return loaderFactory;
}
Expand Down
31 changes: 30 additions & 1 deletion library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.Build;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter;
Expand Down Expand Up @@ -29,6 +30,7 @@ public class GlideBuilder {
private DiskCache diskCache;
private ExecutorService sourceService;
private ExecutorService diskCacheService;
private DecodeFormat decodeFormat;

public GlideBuilder(Context context) {
this.context = context.getApplicationContext();
Expand Down Expand Up @@ -110,6 +112,29 @@ public GlideBuilder setDiskCacheService(ExecutorService service) {
return this;
}

/**
* Sets the {@link com.bumptech.glide.load.DecodeFormat} that will be the default format for all the default
* decoders that can change the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap}s they
* decode.
*
* <p>
* Decode format is always a suggestion, not a requirement. See {@link com.bumptech.glide.load.DecodeFormat} for
* more details.
* </p>
*
* <p>
* If you instantiate and use a custom decoder, it will use
* {@link com.bumptech.glide.load.DecodeFormat#DEFAULT} as its default.
* </p>
*
* @param decodeFormat The format to use.
* @return This builder.
*/
public GlideBuilder setDecodeFormat(DecodeFormat decodeFormat) {
this.decodeFormat = decodeFormat;
return this;
}

// For testing.
GlideBuilder setEngine(Engine engine) {
this.engine = engine;
Expand Down Expand Up @@ -152,6 +177,10 @@ Glide createGlide() {
engine = new Engine(memoryCache, diskCache, diskCacheService, sourceService);
}

return new Glide(engine, memoryCache, bitmapPool, context);
if (decodeFormat == null) {
decodeFormat = DecodeFormat.DEFAULT;
}

return new Glide(engine, memoryCache, bitmapPool, context, decodeFormat);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.load;

import android.os.Build;

/**
* Options for setting the value of {@link android.graphics.Bitmap#getConfig()} for {@link android.graphics.Bitmap}s
* returned by a {@link com.bumptech.glide.load.resource.bitmap.BitmapDecoder}.
Expand All @@ -25,5 +27,9 @@ public enum DecodeFormat {
* {@link android.graphics.Bitmap.Config#RGB_565} for {@link android.graphics.Bitmap#getConfig()}.
*
*/
PREFER_RGB_565,
PREFER_RGB_565;

/** The default value for DecodeFormat. */
public static final DecodeFormat DEFAULT = Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT
? ALWAYS_ARGB_8888 : PREFER_RGB_565;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import android.os.ParcelFileDescriptor;

import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.provider.DataLoadProvider;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
Expand All @@ -23,9 +24,9 @@ public class FileDescriptorBitmapDataLoadProvider implements DataLoadProvider<Pa
private final BitmapEncoder encoder;
private final Encoder<ParcelFileDescriptor> sourceEncoder;

public FileDescriptorBitmapDataLoadProvider(BitmapPool bitmapPool) {
cacheDecoder = new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(bitmapPool));
sourceDecoder = new FileDescriptorBitmapDecoder(bitmapPool);
public FileDescriptorBitmapDataLoadProvider(BitmapPool bitmapPool, DecodeFormat decodeFormat) {
cacheDecoder = new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(bitmapPool, decodeFormat));
sourceDecoder = new FileDescriptorBitmapDecoder(bitmapPool, decodeFormat);
encoder = new BitmapEncoder();
sourceEncoder = NullEncoder.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.bumptech.glide.load.resource.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.ParcelFileDescriptor;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
Expand All @@ -18,8 +20,16 @@ public class FileDescriptorBitmapDecoder implements ResourceDecoder<ParcelFileDe
private final BitmapPool bitmapPool;
private DecodeFormat decodeFormat;

public FileDescriptorBitmapDecoder(BitmapPool bitmapPool) {
this(new VideoBitmapDecoder(), bitmapPool, DecodeFormat.PREFER_RGB_565);
public FileDescriptorBitmapDecoder(Context context) {
this(Glide.get(context).getBitmapPool(), DecodeFormat.DEFAULT);
}

public FileDescriptorBitmapDecoder(Context context, DecodeFormat decodeFormat) {
this(Glide.get(context).getBitmapPool(), decodeFormat);
}

public FileDescriptorBitmapDecoder(BitmapPool bitmapPool, DecodeFormat decodeFormat) {
this(new VideoBitmapDecoder(), bitmapPool, decodeFormat);
}

public FileDescriptorBitmapDecoder(VideoBitmapDecoder bitmapDecoder, BitmapPool bitmapPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import android.graphics.Bitmap;
import android.os.ParcelFileDescriptor;

import com.bumptech.glide.provider.DataLoadProvider;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.model.ImageVideoWrapperEncoder;
import com.bumptech.glide.load.model.StreamEncoder;
import com.bumptech.glide.load.resource.NullEncoder;
import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
import com.bumptech.glide.provider.DataLoadProvider;

import java.io.File;
import java.io.InputStream;

/**
* A {@link com.bumptech.glide.provider.DataLoadProvider} for loading either an {@link java.io.InputStream} or an
Expand All @@ -23,16 +19,17 @@
public class ImageVideoDataLoadProvider implements DataLoadProvider<ImageVideoWrapper, Bitmap> {
private final ImageVideoBitmapDecoder sourceDecoder;
private final ResourceDecoder<File, Bitmap> cacheDecoder;
private final BitmapEncoder encoder;
private final ResourceEncoder<Bitmap> encoder;
private final ImageVideoWrapperEncoder sourceEncoder;

public ImageVideoDataLoadProvider(BitmapPool bitmapPool) {
encoder = new BitmapEncoder();
Encoder<ParcelFileDescriptor> fileDescriptorEncoder = NullEncoder.get();
sourceEncoder = new ImageVideoWrapperEncoder(new StreamEncoder(), fileDescriptorEncoder);
StreamBitmapDecoder streamDecoder = new StreamBitmapDecoder(bitmapPool);
cacheDecoder = new FileToStreamDecoder<Bitmap>(streamDecoder);
sourceDecoder = new ImageVideoBitmapDecoder(streamDecoder, new FileDescriptorBitmapDecoder(bitmapPool));
public ImageVideoDataLoadProvider(DataLoadProvider<InputStream, Bitmap> streamBitmapProvider,
DataLoadProvider<ParcelFileDescriptor, Bitmap> fileDescriptorBitmapProvider) {
encoder = streamBitmapProvider.getEncoder();
sourceEncoder = new ImageVideoWrapperEncoder(streamBitmapProvider.getSourceEncoder(),
fileDescriptorBitmapProvider.getSourceEncoder());
cacheDecoder = streamBitmapProvider.getCacheDecoder();
sourceDecoder = new ImageVideoBitmapDecoder(streamBitmapProvider.getSourceDecoder(),
fileDescriptorBitmapProvider.getSourceDecoder());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Bitmap;

import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.provider.DataLoadProvider;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
Expand All @@ -23,9 +24,9 @@ public class StreamBitmapDataLoadProvider implements DataLoadProvider<InputStrea
private final StreamEncoder sourceEncoder;
private final FileToStreamDecoder<Bitmap> cacheDecoder;

public StreamBitmapDataLoadProvider(BitmapPool bitmapPool) {
public StreamBitmapDataLoadProvider(BitmapPool bitmapPool, DecodeFormat decodeFormat) {
sourceEncoder = new StreamEncoder();
decoder = new StreamBitmapDecoder(bitmapPool);
decoder = new StreamBitmapDecoder(bitmapPool, decodeFormat);
encoder = new BitmapEncoder();
cacheDecoder = new FileToStreamDecoder<Bitmap>(decoder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public StreamBitmapDecoder(Context context) {
}

public StreamBitmapDecoder(BitmapPool bitmapPool) {
this(Downsampler.AT_LEAST, bitmapPool, DecodeFormat.PREFER_RGB_565);
this(bitmapPool, DecodeFormat.DEFAULT);
}

public StreamBitmapDecoder(Context context, DecodeFormat decodeFormat) {
this(Glide.get(context).getBitmapPool(), decodeFormat);
}

public StreamBitmapDecoder(BitmapPool bitmapPool, DecodeFormat decodeFormat) {
this(Downsampler.AT_LEAST, bitmapPool, decodeFormat);
}

public StreamBitmapDecoder(Downsampler downsampler, BitmapPool bitmapPool, DecodeFormat decodeFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.provider.DataLoadProvider;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
Expand All @@ -23,8 +24,8 @@ public class GifDrawableLoadProvider implements DataLoadProvider<InputStream, Gi
private final StreamEncoder sourceEncoder;
private final FileToStreamDecoder<GifDrawable> cacheDecoder;

public GifDrawableLoadProvider(Context context, BitmapPool bitmapPool) {
decoder = new GifResourceDecoder(context, bitmapPool);
public GifDrawableLoadProvider(Context context, BitmapPool bitmapPool, DecodeFormat decodeFormat) {
decoder = new GifResourceDecoder(context, bitmapPool, decodeFormat);
cacheDecoder = new FileToStreamDecoder<GifDrawable>(decoder);
encoder = new GifResourceEncoder(bitmapPool);
sourceEncoder = new StreamEncoder();
Expand Down
Loading

0 comments on commit 162c0ca

Please sign in to comment.