Skip to content

Commit

Permalink
Add method to specify animation executor and fix consistency issues.
Browse files Browse the repository at this point in the history
Progress towards #2471.
  • Loading branch information
sjudd committed Nov 22, 2017
1 parent e51a013 commit 6837543
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
68 changes: 59 additions & 9 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class GlideBuilder {
private RequestOptions defaultRequestOptions = new RequestOptions();
@Nullable
private RequestManagerFactory requestManagerFactory;
private GlideExecutor animationExecutor;

/**
* Sets the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} implementation to use
Expand Down Expand Up @@ -116,39 +117,84 @@ public GlideBuilder setDiskCache(DiskCache.Factory diskCacheFactory) {
}

/**
* Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
* Sets the {@link GlideExecutor} to use when retrieving
* {@link com.bumptech.glide.load.engine.Resource}s that are not already in the cache.
*
* <p> Any implementation must order requests based on their {@link com.bumptech.glide.Priority}
* for thumbnail requests to work properly.
* <p>The thread count defaults to the number of cores available on the device, with a maximum of
* 4.
*
* <p>Use the {@link GlideExecutor#newSourceExecutor()} methods if you'd like to specify options
* for the source executor.
*
* @param service The ExecutorService to use.
* @return This builder.
* @see #setDiskCacheExecutor(GlideExecutor)
* @see GlideExecutor
*
* @deprecated Use {@link #setSourceExecutor(GlideExecutor)}
*/
@Deprecated
public GlideBuilder setResizeExecutor(GlideExecutor service) {
return setSourceExecutor(service);
}

/**
* Sets the {@link GlideExecutor} to use when retrieving
* {@link com.bumptech.glide.load.engine.Resource}s that are not already in the cache.
*
* <p>The thread count defaults to the number of cores available on the device, with a maximum of
* 4.
*
* <p>Use the {@link GlideExecutor#newSourceExecutor()} methods if you'd like to specify options
* for the source executor.
*
* @param service The ExecutorService to use.
* @return This builder.
* @see #setDiskCacheExecutor(GlideExecutor)
* @see GlideExecutor
*/
public GlideBuilder setSourceExecutor(GlideExecutor service) {
this.sourceExecutor = service;
return this;
}

/**
* Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
* {@link com.bumptech.glide.load.engine.Resource}s that are currently in cache.
* Sets the {@link GlideExecutor} to use when retrieving
* {@link com.bumptech.glide.load.engine.Resource}s that are currently in Glide's disk caches.
*
* <p> Any implementation must order requests based on their {@link com.bumptech.glide.Priority}
* for thumbnail requests to work properly.
* <p>Defaults to a single thread which is usually the best combination of memory usage,
* jank, and performance, even on high end devices.
*
* @param service The ExecutorService to use.
* <p>Use the {@link GlideExecutor#newDiskCacheExecutor()} if you'd like to specify options
* for the disk cache executor.
*
* @param service The {@link GlideExecutor} to use.
* @return This builder.
* @see #setResizeExecutor(GlideExecutor)
* @see #setSourceExecutor(GlideExecutor)
* @see GlideExecutor
*/
public GlideBuilder setDiskCacheExecutor(GlideExecutor service) {
this.diskCacheExecutor = service;
return this;
}

/**
* Sets the {@link GlideExecutor} to use when loading frames of animated images and particularly
* of {@link com.bumptech.glide.load.resource.gif.GifDrawable}s.
*
* <p>Defaults to one or two threads, depending on the number of cores available.
*
* <p>Use the {@link GlideExecutor#newAnimationExecutor()} methods if you'd like to specify
* options for the animation executor.
*
* @param service The {@link GlideExecutor} to use.
* @return This builder.
*/
public GlideBuilder setAnimationExecutor(GlideExecutor service) {
this.animationExecutor = service;
return this;
}

/**
* Sets the default {@link RequestOptions} to use for all loads across the app.
*
Expand Down Expand Up @@ -299,6 +345,10 @@ public Glide build(Context context) {
diskCacheExecutor = GlideExecutor.newDiskCacheExecutor();
}

if (animationExecutor == null) {
animationExecutor = GlideExecutor.newAnimationExecutor();
}

if (memorySizeCalculator == null) {
memorySizeCalculator = new MemorySizeCalculator.Builder(context).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ public static GlideExecutor newUnlimitedSourceExecutor() {
false)));
}

/**
* Returns a new cached thread pool that defaults to either one or two threads depending on the
* number of available cores to use when loading frames of animations.
*/
public static GlideExecutor newAnimationExecutor() {
int bestThreadCount = calculateBestThreadCount();
// We don't want to add a ton of threads running animations in parallel with our source and
Expand All @@ -229,16 +233,28 @@ public static GlideExecutor newAnimationExecutor() {
// with more cores, two threads can provide better performance if lots of GIFs are showing at
// once.
int maximumPoolSize = bestThreadCount >= 4 ? 2 : 1;
return new GlideExecutor(

return newAnimationExecutor(maximumPoolSize, UncaughtThrowableStrategy.DEFAULT);
}

/**
* Returns a new cached thread pool with the given thread count and
* {@link UncaughtThrowableStrategy} to use when loading frames of animations.
*/
// Public API.
@SuppressWarnings("WeakerAccess")
public static GlideExecutor newAnimationExecutor(
int threadCount, UncaughtThrowableStrategy uncaughtThrowableStrategy) {
return new GlideExecutor(
new ThreadPoolExecutor(
0 /* corePoolSize */,
maximumPoolSize,
threadCount,
KEEP_ALIVE_TIME_MS,
TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<Runnable>(),
new DefaultThreadFactory(
ANIMATION_EXECUTOR_NAME,
UncaughtThrowableStrategy.DEFAULT,
uncaughtThrowableStrategy,
true)));
}

Expand Down

0 comments on commit 6837543

Please sign in to comment.