Skip to content

Commit 6417b3e

Browse files
falhassenglide-copybara-robot
authored andcommitted
Allow for modification of OS thread priority for Glide threads via Glide Options
PiperOrigin-RevId: 767679718
1 parent 8a23b1e commit 6417b3e

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,17 @@ public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled) {
487487
return this;
488488
}
489489

490+
/**
491+
* Override the OS thread priority of threads created in {@link
492+
* com.bumptech.glide.load.engine.executor.GlideExecutor}.
493+
*
494+
* <p>This is an experimental API that may be removed in the future.
495+
*/
496+
public GlideBuilder setOverrideGlideThreadPriority(boolean isEnabled) {
497+
glideExperimentsBuilder.update(new OverrideGlideThreadPriority(), isEnabled);
498+
return this;
499+
}
500+
490501
/**
491502
* @deprecated This method does nothing. It will be hard coded and removed in a future release
492503
* without further warning.
@@ -622,4 +633,7 @@ static final class EnableImageDecoderForBitmaps implements Experiment {}
622633

623634
/** See {@link #setLogRequestOrigins(boolean)}. */
624635
public static final class LogRequestOrigins implements Experiment {}
636+
637+
/** See {@link #setOverrideGlideThreadPriority(boolean)}. */
638+
public static final class OverrideGlideThreadPriority implements Experiment {}
625639
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
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;
7+
import androidx.annotation.Nullable;
68
import androidx.core.util.Pools;
9+
import com.bumptech.glide.GlideBuilder.OverrideGlideThreadPriority;
710
import com.bumptech.glide.GlideContext;
11+
import com.bumptech.glide.GlideExperiments;
812
import com.bumptech.glide.Priority;
913
import com.bumptech.glide.Registry;
1014
import com.bumptech.glide.load.DataSource;
1115
import com.bumptech.glide.load.EncodeStrategy;
1216
import com.bumptech.glide.load.Key;
17+
import com.bumptech.glide.load.Option;
1318
import com.bumptech.glide.load.Options;
1419
import com.bumptech.glide.load.ResourceEncoder;
1520
import com.bumptech.glide.load.Transformation;
@@ -25,6 +30,7 @@
2530
import java.util.ArrayList;
2631
import java.util.List;
2732
import java.util.Map;
33+
import java.util.function.Supplier;
2834

2935
/**
3036
* A class responsible for decoding resources either from cached data or from the original source
@@ -41,6 +47,10 @@ class DecodeJob<R>
4147
Comparable<DecodeJob<?>>,
4248
Poolable {
4349
private static final String TAG = "DecodeJob";
50+
private static final Option<Supplier<Integer>> GLIDE_THREAD_PRIORITY =
51+
Option.memory("glide_thread_priority");
52+
private static final int DEFAULT_THREAD_PRIORITY =
53+
Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE;
4454

4555
private final DecodeHelper<R> decodeHelper = new DecodeHelper<>();
4656
private final List<Throwable> throwables = new ArrayList<>();
@@ -65,6 +75,8 @@ class DecodeJob<R>
6575
private long startFetchTime;
6676
private boolean onlyRetrieveFromCache;
6777
private Object model;
78+
private GlideExperiments experiments;
79+
@Nullable private Supplier<Integer> glideThreadPriorityOverride;
6880

6981
private Thread currentThread;
7082
private Key currentSourceKey;
@@ -129,6 +141,8 @@ DecodeJob<R> init(
129141
this.order = order;
130142
this.runReason = RunReason.INITIALIZE;
131143
this.model = model;
144+
this.experiments = glideContext.getExperiments();
145+
this.glideThreadPriorityOverride = options.get(GLIDE_THREAD_PRIORITY);
132146
return this;
133147
}
134148

@@ -326,7 +340,20 @@ private void runGenerators() {
326340
// onDataFetcherReady.
327341
}
328342

343+
/**
344+
* Restores the OS priority of the Glide thread to the default thread priority of {@link
345+
* com.bumptech.glide.load.engine.executor.GlideExecutor}.
346+
*/
347+
private void restoreThreadPriority() {
348+
if (experiments.isEnabled(OverrideGlideThreadPriority.class)
349+
&& glideThreadPriorityOverride != null
350+
&& glideThreadPriorityOverride.get() != null) {
351+
Process.setThreadPriority(Process.myTid(), DEFAULT_THREAD_PRIORITY);
352+
}
353+
}
354+
329355
private void notifyFailed() {
356+
restoreThreadPriority();
330357
setNotifiedOrThrow();
331358
GlideException e = new GlideException("Failed to load resource", new ArrayList<>(throwables));
332359
callback.onLoadFailed(e);
@@ -335,6 +362,7 @@ private void notifyFailed() {
335362

336363
private void notifyComplete(
337364
Resource<R> resource, DataSource dataSource, boolean isLoadedFromAlternateCacheKey) {
365+
restoreThreadPriority();
338366
setNotifiedOrThrow();
339367
callback.onResourceReady(resource, dataSource, isLoadedFromAlternateCacheKey);
340368
}
@@ -429,6 +457,11 @@ private void decodeFromRetrievedData() {
429457
+ ", fetcher: "
430458
+ currentFetcher);
431459
}
460+
if (experiments.isEnabled(OverrideGlideThreadPriority.class)
461+
&& glideThreadPriorityOverride != null
462+
&& glideThreadPriorityOverride.get() != null) {
463+
Process.setThreadPriority(Process.myTid(), glideThreadPriorityOverride.get().intValue());
464+
}
432465
Resource<R> resource = null;
433466
try {
434467
resource = decodeFromData(currentFetcher, currentData, currentDataSource);

0 commit comments

Comments
 (0)