Skip to content

Commit f810ee1

Browse files
falhassenglide-copybara-robot
authored andcommitted
Add new functionality to Glide to allow for modification of OS thread priority for Glide threads
PiperOrigin-RevId: 767679718
1 parent 8a23b1e commit f810ee1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

library/src/main/java/com/bumptech/glide/load/engine/DecodeJob.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bumptech.glide.load.engine;
22

33
import android.os.Build;
4+
import android.os.Process;
45
import android.util.Log;
56
import androidx.annotation.NonNull;
67
import androidx.core.util.Pools;
@@ -10,6 +11,7 @@
1011
import com.bumptech.glide.load.DataSource;
1112
import com.bumptech.glide.load.EncodeStrategy;
1213
import com.bumptech.glide.load.Key;
14+
import com.bumptech.glide.load.Option;
1315
import com.bumptech.glide.load.Options;
1416
import com.bumptech.glide.load.ResourceEncoder;
1517
import com.bumptech.glide.load.Transformation;
@@ -25,6 +27,7 @@
2527
import java.util.ArrayList;
2628
import java.util.List;
2729
import java.util.Map;
30+
import java.util.function.Supplier;
2831

2932
/**
3033
* A class responsible for decoding resources either from cached data or from the original source
@@ -41,6 +44,10 @@ class DecodeJob<R>
4144
Comparable<DecodeJob<?>>,
4245
Poolable {
4346
private static final String TAG = "DecodeJob";
47+
private static final Option<Supplier<Integer>> GLIDE_THREAD_PRIORITY =
48+
Option.memory("glide_thread_priority");
49+
private static final int DEFAULT_THREAD_PRIORITY =
50+
Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE;
4451

4552
private final DecodeHelper<R> decodeHelper = new DecodeHelper<>();
4653
private final List<Throwable> throwables = new ArrayList<>();
@@ -49,6 +56,7 @@ class DecodeJob<R>
4956
private final Pools.Pool<DecodeJob<?>> pool;
5057
private final DeferredEncodeManager<?> deferredEncodeManager = new DeferredEncodeManager<>();
5158
private final ReleaseManager releaseManager = new ReleaseManager();
59+
private final int threadPriority = Process.getThreadPriority(Process.myTid());
5260

5361
private GlideContext glideContext;
5462
private Key signature;
@@ -65,6 +73,7 @@ class DecodeJob<R>
6573
private long startFetchTime;
6674
private boolean onlyRetrieveFromCache;
6775
private Object model;
76+
private Supplier<Integer> glideThreadPriorityOverride;
6877

6978
private Thread currentThread;
7079
private Key currentSourceKey;
@@ -129,6 +138,7 @@ DecodeJob<R> init(
129138
this.order = order;
130139
this.runReason = RunReason.INITIALIZE;
131140
this.model = model;
141+
this.glideThreadPriorityOverride = options.get(GLIDE_THREAD_PRIORITY);
132142
return this;
133143
}
134144

@@ -326,7 +336,17 @@ private void runGenerators() {
326336
// onDataFetcherReady.
327337
}
328338

339+
private void restoreThreadPriority() {
340+
if (glideThreadPriorityOverride != null && glideThreadPriorityOverride.get() != null) {
341+
// Setting to default instead of original priority because threads can run multiple jobs at
342+
// once so if a new job is started before a previous higher priority job completes, that new
343+
// job will start with a higher priority.
344+
Process.setThreadPriority(Process.myTid(), DEFAULT_THREAD_PRIORITY);
345+
}
346+
}
347+
329348
private void notifyFailed() {
349+
restoreThreadPriority();
330350
setNotifiedOrThrow();
331351
GlideException e = new GlideException("Failed to load resource", new ArrayList<>(throwables));
332352
callback.onLoadFailed(e);
@@ -335,6 +355,7 @@ private void notifyFailed() {
335355

336356
private void notifyComplete(
337357
Resource<R> resource, DataSource dataSource, boolean isLoadedFromAlternateCacheKey) {
358+
restoreThreadPriority();
338359
setNotifiedOrThrow();
339360
callback.onResourceReady(resource, dataSource, isLoadedFromAlternateCacheKey);
340361
}
@@ -429,6 +450,17 @@ private void decodeFromRetrievedData() {
429450
+ ", fetcher: "
430451
+ currentFetcher);
431452
}
453+
if (glideThreadPriorityOverride != null && glideThreadPriorityOverride.get() != null) {
454+
Log.d(
455+
"BigDawg",
456+
"Setting thread priority for thread "
457+
+ Process.myTid()
458+
+ " from "
459+
+ threadPriority
460+
+ " to "
461+
+ glideThreadPriorityOverride.get().intValue());
462+
Process.setThreadPriority(Process.myTid(), glideThreadPriorityOverride.get().intValue());
463+
}
432464
Resource<R> resource = null;
433465
try {
434466
resource = decodeFromData(currentFetcher, currentData, currentDataSource);

0 commit comments

Comments
 (0)