Skip to content

Commit 6837543

Browse files
committed
Add method to specify animation executor and fix consistency issues.
Progress towards #2471.
1 parent e51a013 commit 6837543

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

library/src/main/java/com/bumptech/glide/GlideBuilder.java

+59-9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public final class GlideBuilder {
4545
private RequestOptions defaultRequestOptions = new RequestOptions();
4646
@Nullable
4747
private RequestManagerFactory requestManagerFactory;
48+
private GlideExecutor animationExecutor;
4849

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

118119
/**
119-
* Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
120+
* Sets the {@link GlideExecutor} to use when retrieving
120121
* {@link com.bumptech.glide.load.engine.Resource}s that are not already in the cache.
121122
*
122-
* <p> Any implementation must order requests based on their {@link com.bumptech.glide.Priority}
123-
* for thumbnail requests to work properly.
123+
* <p>The thread count defaults to the number of cores available on the device, with a maximum of
124+
* 4.
125+
*
126+
* <p>Use the {@link GlideExecutor#newSourceExecutor()} methods if you'd like to specify options
127+
* for the source executor.
124128
*
125129
* @param service The ExecutorService to use.
126130
* @return This builder.
127131
* @see #setDiskCacheExecutor(GlideExecutor)
128132
* @see GlideExecutor
133+
*
134+
* @deprecated Use {@link #setSourceExecutor(GlideExecutor)}
129135
*/
136+
@Deprecated
130137
public GlideBuilder setResizeExecutor(GlideExecutor service) {
138+
return setSourceExecutor(service);
139+
}
140+
141+
/**
142+
* Sets the {@link GlideExecutor} to use when retrieving
143+
* {@link com.bumptech.glide.load.engine.Resource}s that are not already in the cache.
144+
*
145+
* <p>The thread count defaults to the number of cores available on the device, with a maximum of
146+
* 4.
147+
*
148+
* <p>Use the {@link GlideExecutor#newSourceExecutor()} methods if you'd like to specify options
149+
* for the source executor.
150+
*
151+
* @param service The ExecutorService to use.
152+
* @return This builder.
153+
* @see #setDiskCacheExecutor(GlideExecutor)
154+
* @see GlideExecutor
155+
*/
156+
public GlideBuilder setSourceExecutor(GlideExecutor service) {
131157
this.sourceExecutor = service;
132158
return this;
133159
}
134160

135161
/**
136-
* Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
137-
* {@link com.bumptech.glide.load.engine.Resource}s that are currently in cache.
162+
* Sets the {@link GlideExecutor} to use when retrieving
163+
* {@link com.bumptech.glide.load.engine.Resource}s that are currently in Glide's disk caches.
138164
*
139-
* <p> Any implementation must order requests based on their {@link com.bumptech.glide.Priority}
140-
* for thumbnail requests to work properly.
165+
* <p>Defaults to a single thread which is usually the best combination of memory usage,
166+
* jank, and performance, even on high end devices.
141167
*
142-
* @param service The ExecutorService to use.
168+
* <p>Use the {@link GlideExecutor#newDiskCacheExecutor()} if you'd like to specify options
169+
* for the disk cache executor.
170+
*
171+
* @param service The {@link GlideExecutor} to use.
143172
* @return This builder.
144-
* @see #setResizeExecutor(GlideExecutor)
173+
* @see #setSourceExecutor(GlideExecutor)
145174
* @see GlideExecutor
146175
*/
147176
public GlideBuilder setDiskCacheExecutor(GlideExecutor service) {
148177
this.diskCacheExecutor = service;
149178
return this;
150179
}
151180

181+
/**
182+
* Sets the {@link GlideExecutor} to use when loading frames of animated images and particularly
183+
* of {@link com.bumptech.glide.load.resource.gif.GifDrawable}s.
184+
*
185+
* <p>Defaults to one or two threads, depending on the number of cores available.
186+
*
187+
* <p>Use the {@link GlideExecutor#newAnimationExecutor()} methods if you'd like to specify
188+
* options for the animation executor.
189+
*
190+
* @param service The {@link GlideExecutor} to use.
191+
* @return This builder.
192+
*/
193+
public GlideBuilder setAnimationExecutor(GlideExecutor service) {
194+
this.animationExecutor = service;
195+
return this;
196+
}
197+
152198
/**
153199
* Sets the default {@link RequestOptions} to use for all loads across the app.
154200
*
@@ -299,6 +345,10 @@ public Glide build(Context context) {
299345
diskCacheExecutor = GlideExecutor.newDiskCacheExecutor();
300346
}
301347

348+
if (animationExecutor == null) {
349+
animationExecutor = GlideExecutor.newAnimationExecutor();
350+
}
351+
302352
if (memorySizeCalculator == null) {
303353
memorySizeCalculator = new MemorySizeCalculator.Builder(context).build();
304354
}

library/src/main/java/com/bumptech/glide/load/engine/executor/GlideExecutor.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ public static GlideExecutor newUnlimitedSourceExecutor() {
221221
false)));
222222
}
223223

224+
/**
225+
* Returns a new cached thread pool that defaults to either one or two threads depending on the
226+
* number of available cores to use when loading frames of animations.
227+
*/
224228
public static GlideExecutor newAnimationExecutor() {
225229
int bestThreadCount = calculateBestThreadCount();
226230
// We don't want to add a ton of threads running animations in parallel with our source and
@@ -229,16 +233,28 @@ public static GlideExecutor newAnimationExecutor() {
229233
// with more cores, two threads can provide better performance if lots of GIFs are showing at
230234
// once.
231235
int maximumPoolSize = bestThreadCount >= 4 ? 2 : 1;
232-
return new GlideExecutor(
236+
237+
return newAnimationExecutor(maximumPoolSize, UncaughtThrowableStrategy.DEFAULT);
238+
}
239+
240+
/**
241+
* Returns a new cached thread pool with the given thread count and
242+
* {@link UncaughtThrowableStrategy} to use when loading frames of animations.
243+
*/
244+
// Public API.
245+
@SuppressWarnings("WeakerAccess")
246+
public static GlideExecutor newAnimationExecutor(
247+
int threadCount, UncaughtThrowableStrategy uncaughtThrowableStrategy) {
248+
return new GlideExecutor(
233249
new ThreadPoolExecutor(
234250
0 /* corePoolSize */,
235-
maximumPoolSize,
251+
threadCount,
236252
KEEP_ALIVE_TIME_MS,
237253
TimeUnit.MILLISECONDS,
238254
new PriorityBlockingQueue<Runnable>(),
239255
new DefaultThreadFactory(
240256
ANIMATION_EXECUTOR_NAME,
241-
UncaughtThrowableStrategy.DEFAULT,
257+
uncaughtThrowableStrategy,
242258
true)));
243259
}
244260

0 commit comments

Comments
 (0)