Skip to content

Commit 6c57a2f

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 6c57a2f

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-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);

library/src/main/java/com/bumptech/glide/util/Executors.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public void execute(@NonNull Runnable command) {
1919
Util.postOnUiThread(command);
2020
}
2121
};
22+
private static final Executor MAIN_THREAD_EXECUTOR_FRONT =
23+
new Executor() {
24+
@Override
25+
public void execute(@NonNull Runnable command) {
26+
Util.postAtFrontOfQueueOnUiThread(command);
27+
}
28+
};
2229
private static final Executor DIRECT_EXECUTOR =
2330
new Executor() {
2431
@Override
@@ -32,6 +39,11 @@ public static Executor mainThreadExecutor() {
3239
return MAIN_THREAD_EXECUTOR;
3340
}
3441

42+
/** Posts executions to the main thread at the front of the queue. */
43+
public static Executor mainThreadExecutorFront() {
44+
return MAIN_THREAD_EXECUTOR_FRONT;
45+
}
46+
3547
/** Immediately calls {@link Runnable#run()} on the current thread. */
3648
public static Executor directExecutor() {
3749
return DIRECT_EXECUTOR;

library/src/main/java/com/bumptech/glide/util/Util.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ public static void postOnUiThread(Runnable runnable) {
151151
getUiThreadHandler().post(runnable);
152152
}
153153

154+
/**
155+
* Posts the given {@code runnable} to the front of the queue on the UI thread using a shared
156+
* {@link Handler}.
157+
*/
158+
public static void postAtFrontOfQueueOnUiThread(Runnable runnable) {
159+
getUiThreadHandler().postAtFrontOfQueue(runnable);
160+
}
161+
154162
/** Removes the given {@code runnable} from the UI threads queue if it is still queued. */
155163
public static void removeCallbacksOnUiThread(Runnable runnable) {
156164
getUiThreadHandler().removeCallbacks(runnable);

0 commit comments

Comments
 (0)