1
1
package com .bumptech .glide .load .engine ;
2
2
3
3
import android .os .Build ;
4
+ import android .os .Process ;
4
5
import android .util .Log ;
5
6
import androidx .annotation .NonNull ;
7
+ import androidx .annotation .Nullable ;
6
8
import androidx .core .util .Pools ;
9
+ import com .bumptech .glide .GlideBuilder .OverrideGlideThreadPriority ;
7
10
import com .bumptech .glide .GlideContext ;
11
+ import com .bumptech .glide .GlideExperiments ;
8
12
import com .bumptech .glide .Priority ;
9
13
import com .bumptech .glide .Registry ;
10
14
import com .bumptech .glide .load .DataSource ;
11
15
import com .bumptech .glide .load .EncodeStrategy ;
12
16
import com .bumptech .glide .load .Key ;
17
+ import com .bumptech .glide .load .Option ;
13
18
import com .bumptech .glide .load .Options ;
14
19
import com .bumptech .glide .load .ResourceEncoder ;
15
20
import com .bumptech .glide .load .Transformation ;
25
30
import java .util .ArrayList ;
26
31
import java .util .List ;
27
32
import java .util .Map ;
33
+ import java .util .function .Supplier ;
28
34
29
35
/**
30
36
* A class responsible for decoding resources either from cached data or from the original source
@@ -41,6 +47,10 @@ class DecodeJob<R>
41
47
Comparable <DecodeJob <?>>,
42
48
Poolable {
43
49
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 ;
44
54
45
55
private final DecodeHelper <R > decodeHelper = new DecodeHelper <>();
46
56
private final List <Throwable > throwables = new ArrayList <>();
@@ -65,6 +75,8 @@ class DecodeJob<R>
65
75
private long startFetchTime ;
66
76
private boolean onlyRetrieveFromCache ;
67
77
private Object model ;
78
+ private GlideExperiments experiments ;
79
+ @ Nullable private Supplier <Integer > glideThreadPriorityOverride ;
68
80
69
81
private Thread currentThread ;
70
82
private Key currentSourceKey ;
@@ -129,6 +141,8 @@ DecodeJob<R> init(
129
141
this .order = order ;
130
142
this .runReason = RunReason .INITIALIZE ;
131
143
this .model = model ;
144
+ this .experiments = glideContext .getExperiments ();
145
+ this .glideThreadPriorityOverride = options .get (GLIDE_THREAD_PRIORITY );
132
146
return this ;
133
147
}
134
148
@@ -326,7 +340,20 @@ private void runGenerators() {
326
340
// onDataFetcherReady.
327
341
}
328
342
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
+
329
355
private void notifyFailed () {
356
+ restoreThreadPriority ();
330
357
setNotifiedOrThrow ();
331
358
GlideException e = new GlideException ("Failed to load resource" , new ArrayList <>(throwables ));
332
359
callback .onLoadFailed (e );
@@ -335,6 +362,7 @@ private void notifyFailed() {
335
362
336
363
private void notifyComplete (
337
364
Resource <R > resource , DataSource dataSource , boolean isLoadedFromAlternateCacheKey ) {
365
+ restoreThreadPriority ();
338
366
setNotifiedOrThrow ();
339
367
callback .onResourceReady (resource , dataSource , isLoadedFromAlternateCacheKey );
340
368
}
@@ -429,6 +457,11 @@ private void decodeFromRetrievedData() {
429
457
+ ", fetcher: "
430
458
+ currentFetcher );
431
459
}
460
+ if (experiments .isEnabled (OverrideGlideThreadPriority .class )
461
+ && glideThreadPriorityOverride != null
462
+ && glideThreadPriorityOverride .get () != null ) {
463
+ Process .setThreadPriority (Process .myTid (), glideThreadPriorityOverride .get ().intValue ());
464
+ }
432
465
Resource <R > resource = null ;
433
466
try {
434
467
resource = decodeFromData (currentFetcher , currentData , currentDataSource );
0 commit comments