提交 f7a939ec 编写于 作者: S Sam Judd

Avoid some unnecessary object allocations initializing Glide.

上级 90f1cc93
...@@ -300,43 +300,66 @@ public class Glide implements ComponentCallbacks2 { ...@@ -300,43 +300,66 @@ public class Glide implements ComponentCallbacks2 {
resources.getDisplayMetrics(), bitmapPool, arrayPool); resources.getDisplayMetrics(), bitmapPool, arrayPool);
ByteBufferGifDecoder byteBufferGifDecoder = ByteBufferGifDecoder byteBufferGifDecoder =
new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), bitmapPool, arrayPool); new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), bitmapPool, arrayPool);
VideoBitmapDecoder videoBitmapDecoder = new VideoBitmapDecoder(bitmapPool);
ByteBufferBitmapDecoder byteBufferBitmapDecoder = new ByteBufferBitmapDecoder(downsampler);
StreamBitmapDecoder streamBitmapDecoder = new StreamBitmapDecoder(downsampler, arrayPool);
ResourceDrawableDecoder resourceDrawableDecoder =
new ResourceDrawableDecoder(context, bitmapPool);
ResourceLoader.StreamFactory resourceLoaderStreamFactory =
new ResourceLoader.StreamFactory(resources);
ResourceLoader.UriFactory resourceLoaderUriFactory =
new ResourceLoader.UriFactory(resources);
ResourceLoader.FileDescriptorFactory resourceLoaderFileDescriptorFactory =
new ResourceLoader.FileDescriptorFactory(resources);
BitmapEncoder bitmapEncoder = new BitmapEncoder();
registry registry
.append(ByteBuffer.class, new ByteBufferEncoder()) .append(ByteBuffer.class, new ByteBufferEncoder())
.append(InputStream.class, new StreamEncoder(arrayPool)) .append(InputStream.class, new StreamEncoder(arrayPool))
/* Bitmaps */ /* Bitmaps */
.append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, .append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder)
new ByteBufferBitmapDecoder(downsampler)) .append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, streamBitmapDecoder)
.append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, .append(
new StreamBitmapDecoder(downsampler, arrayPool)) Registry.BUCKET_BITMAP, ParcelFileDescriptor.class, Bitmap.class, videoBitmapDecoder)
.append(Registry.BUCKET_BITMAP, ParcelFileDescriptor.class, Bitmap.class, .append(Bitmap.class, bitmapEncoder)
new VideoBitmapDecoder(bitmapPool)) /* BitmapDrawables */
.append(Bitmap.class, new BitmapEncoder()) .append(
/* GlideBitmapDrawables */ Registry.BUCKET_BITMAP_DRAWABLE,
.append(Registry.BUCKET_BITMAP_DRAWABLE, ByteBuffer.class, BitmapDrawable.class, ByteBuffer.class,
new BitmapDrawableDecoder<>(resources, bitmapPool, BitmapDrawable.class,
new ByteBufferBitmapDecoder(downsampler))) new BitmapDrawableDecoder<>(resources, bitmapPool, byteBufferBitmapDecoder))
.append(Registry.BUCKET_BITMAP_DRAWABLE, InputStream.class, BitmapDrawable.class, .append(
new BitmapDrawableDecoder<>(resources, bitmapPool, Registry.BUCKET_BITMAP_DRAWABLE,
new StreamBitmapDecoder(downsampler, arrayPool))) InputStream.class,
.append(Registry.BUCKET_BITMAP_DRAWABLE, ParcelFileDescriptor.class, BitmapDrawable.class, BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, bitmapPool, new VideoBitmapDecoder(bitmapPool))) new BitmapDrawableDecoder<>(resources, bitmapPool, streamBitmapDecoder))
.append(BitmapDrawable.class, new BitmapDrawableEncoder(bitmapPool, new BitmapEncoder())) .append(
Registry.BUCKET_BITMAP_DRAWABLE,
ParcelFileDescriptor.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, bitmapPool, videoBitmapDecoder))
.append(BitmapDrawable.class, new BitmapDrawableEncoder(bitmapPool, bitmapEncoder))
/* GIFs */ /* GIFs */
.append(Registry.BUCKET_GIF, InputStream.class, GifDrawable.class, .append(
Registry.BUCKET_GIF,
InputStream.class,
GifDrawable.class,
new StreamGifDecoder(registry.getImageHeaderParsers(), byteBufferGifDecoder, arrayPool)) new StreamGifDecoder(registry.getImageHeaderParsers(), byteBufferGifDecoder, arrayPool))
.append(Registry.BUCKET_GIF, ByteBuffer.class, GifDrawable.class, byteBufferGifDecoder) .append(Registry.BUCKET_GIF, ByteBuffer.class, GifDrawable.class, byteBufferGifDecoder)
.append(GifDrawable.class, new GifDrawableEncoder()) .append(GifDrawable.class, new GifDrawableEncoder())
/* GIF Frames */ /* GIF Frames */
// Compilation with Gradle requires the type to be specified for UnitModelLoader here. // Compilation with Gradle requires the type to be specified for UnitModelLoader here.
.append(GifDecoder.class, GifDecoder.class, new UnitModelLoader.Factory<GifDecoder>()) .append(
.append(Registry.BUCKET_BITMAP, GifDecoder.class, Bitmap.class, GifDecoder.class, GifDecoder.class, UnitModelLoader.Factory.<GifDecoder>getInstance())
.append(
Registry.BUCKET_BITMAP,
GifDecoder.class,
Bitmap.class,
new GifFrameResourceDecoder(bitmapPool)) new GifFrameResourceDecoder(bitmapPool))
/* Drawables */ /* Drawables */
.append(Uri.class, Drawable.class, new ResourceDrawableDecoder(context, bitmapPool)) .append(Uri.class, Drawable.class, resourceDrawableDecoder)
.append(Uri.class, Bitmap.class, .append(
new ResourceBitmapDecoder( Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitmapPool))
new ResourceDrawableDecoder(context, bitmapPool), bitmapPool))
/* Files */ /* Files */
.register(new ByteBufferRewinder.Factory()) .register(new ByteBufferRewinder.Factory())
.append(File.class, ByteBuffer.class, new ByteBufferFileLoader.Factory()) .append(File.class, ByteBuffer.class, new ByteBufferFileLoader.Factory())
...@@ -344,21 +367,21 @@ public class Glide implements ComponentCallbacks2 { ...@@ -344,21 +367,21 @@ public class Glide implements ComponentCallbacks2 {
.append(File.class, File.class, new FileDecoder()) .append(File.class, File.class, new FileDecoder())
.append(File.class, ParcelFileDescriptor.class, new FileLoader.FileDescriptorFactory()) .append(File.class, ParcelFileDescriptor.class, new FileLoader.FileDescriptorFactory())
// Compilation with Gradle requires the type to be specified for UnitModelLoader here. // Compilation with Gradle requires the type to be specified for UnitModelLoader here.
.append(File.class, File.class, new UnitModelLoader.Factory<File>()) .append(File.class, File.class, UnitModelLoader.Factory.<File>getInstance())
/* Models */ /* Models */
.register(new InputStreamRewinder.Factory(arrayPool)) .register(new InputStreamRewinder.Factory(arrayPool))
.append(int.class, InputStream.class, new ResourceLoader.StreamFactory(resources)) .append(int.class, InputStream.class, resourceLoaderStreamFactory)
.append( .append(
int.class, int.class,
ParcelFileDescriptor.class, ParcelFileDescriptor.class,
new ResourceLoader.FileDescriptorFactory(resources)) resourceLoaderFileDescriptorFactory)
.append(Integer.class, InputStream.class, new ResourceLoader.StreamFactory(resources)) .append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
.append( .append(
Integer.class, Integer.class,
ParcelFileDescriptor.class, ParcelFileDescriptor.class,
new ResourceLoader.FileDescriptorFactory(resources)) resourceLoaderFileDescriptorFactory)
.append(Integer.class, Uri.class, new ResourceLoader.UriFactory(resources)) .append(Integer.class, Uri.class, resourceLoaderUriFactory)
.append(int.class, Uri.class, new ResourceLoader.UriFactory(resources)) .append(int.class, Uri.class, resourceLoaderUriFactory)
.append(String.class, InputStream.class, new DataUrlLoader.StreamFactory()) .append(String.class, InputStream.class, new DataUrlLoader.StreamFactory())
.append(String.class, InputStream.class, new StringLoader.StreamFactory()) .append(String.class, InputStream.class, new StringLoader.StreamFactory())
.append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory()) .append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory())
...@@ -382,9 +405,11 @@ public class Glide implements ComponentCallbacks2 { ...@@ -382,9 +405,11 @@ public class Glide implements ComponentCallbacks2 {
.append(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory()) .append(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory())
.append(byte[].class, ByteBuffer.class, new ByteArrayLoader.ByteBufferFactory()) .append(byte[].class, ByteBuffer.class, new ByteArrayLoader.ByteBufferFactory())
.append(byte[].class, InputStream.class, new ByteArrayLoader.StreamFactory()) .append(byte[].class, InputStream.class, new ByteArrayLoader.StreamFactory())
.append(Uri.class, Uri.class, new UnitModelLoader.Factory<Uri>()) .append(Uri.class, Uri.class, UnitModelLoader.Factory.<Uri>getInstance())
/* Transcoders */ /* Transcoders */
.register(Bitmap.class, BitmapDrawable.class, .register(
Bitmap.class,
BitmapDrawable.class,
new BitmapDrawableTranscoder(resources, bitmapPool)) new BitmapDrawableTranscoder(resources, bitmapPool))
.register(Bitmap.class, byte[].class, new BitmapBytesTranscoder()) .register(Bitmap.class, byte[].class, new BitmapBytesTranscoder())
.register(GifDrawable.class, byte[].class, new GifDrawableBytesTranscoder()); .register(GifDrawable.class, byte[].class, new GifDrawableBytesTranscoder());
......
...@@ -111,7 +111,7 @@ public class ResourceLoader<Data> implements ModelLoader<Integer, Data> { ...@@ -111,7 +111,7 @@ public class ResourceLoader<Data> implements ModelLoader<Integer, Data> {
@Override @Override
public ModelLoader<Integer, Uri> build(MultiModelLoaderFactory multiFactory) { public ModelLoader<Integer, Uri> build(MultiModelLoaderFactory multiFactory) {
return new ResourceLoader<>(resources, new UnitModelLoader<Uri>()); return new ResourceLoader<>(resources, UnitModelLoader.<Uri>getInstance());
} }
@Override @Override
......
...@@ -14,6 +14,21 @@ import com.bumptech.glide.signature.ObjectKey; ...@@ -14,6 +14,21 @@ import com.bumptech.glide.signature.ObjectKey;
* @param <Model> The type of model that will also be returned as decodable data. * @param <Model> The type of model that will also be returned as decodable data.
*/ */
public class UnitModelLoader<Model> implements ModelLoader<Model, Model> { public class UnitModelLoader<Model> implements ModelLoader<Model, Model> {
@SuppressWarnings("deprecation")
private static final UnitModelLoader<?> INSTANCE = new UnitModelLoader<>();
@SuppressWarnings("unchecked")
public static <T> UnitModelLoader<T> getInstance() {
return (UnitModelLoader<T>) INSTANCE;
}
/**
* @deprecated Use {@link #getInstance()} instead.
*/
@Deprecated
public UnitModelLoader() {
// Intentionally empty.
}
@Override @Override
public LoadData<Model> buildLoadData(Model model, int width, int height, public LoadData<Model> buildLoadData(Model model, int width, int height,
...@@ -68,11 +83,26 @@ public class UnitModelLoader<Model> implements ModelLoader<Model, Model> { ...@@ -68,11 +83,26 @@ public class UnitModelLoader<Model> implements ModelLoader<Model, Model> {
* *
* @param <Model> The type of model that will also be returned as decodable data. * @param <Model> The type of model that will also be returned as decodable data.
*/ */
// PMD seems to be just wrong here, maybe confused by getInstance in UnitModelLoader.
@SuppressWarnings("PMD.SingleMethodSingleton")
public static class Factory<Model> implements ModelLoaderFactory<Model, Model> { public static class Factory<Model> implements ModelLoaderFactory<Model, Model> {
@SuppressWarnings("deprecation")
private static final Factory<?> FACTORY = new Factory<>();
@SuppressWarnings("unchecked")
public static <T> Factory<T> getInstance() {
return (Factory<T>) FACTORY;
}
/** @deprecated Use {@link #getInstance()} instead. */
@Deprecated
public Factory() {
// Intentionally empty.
}
@Override @Override
public ModelLoader<Model, Model> build(MultiModelLoaderFactory multiFactory) { public ModelLoader<Model, Model> build(MultiModelLoaderFactory multiFactory) {
return new UnitModelLoader<>(); return UnitModelLoader.getInstance();
} }
@Override @Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册