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 ;
6
7
import androidx .core .util .Pools ;
10
11
import com .bumptech .glide .load .DataSource ;
11
12
import com .bumptech .glide .load .EncodeStrategy ;
12
13
import com .bumptech .glide .load .Key ;
14
+ import com .bumptech .glide .load .Option ;
13
15
import com .bumptech .glide .load .Options ;
14
16
import com .bumptech .glide .load .ResourceEncoder ;
15
17
import com .bumptech .glide .load .Transformation ;
25
27
import java .util .ArrayList ;
26
28
import java .util .List ;
27
29
import java .util .Map ;
30
+ import java .util .function .Supplier ;
28
31
29
32
/**
30
33
* A class responsible for decoding resources either from cached data or from the original source
@@ -41,6 +44,10 @@ class DecodeJob<R>
41
44
Comparable <DecodeJob <?>>,
42
45
Poolable {
43
46
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 ;
44
51
45
52
private final DecodeHelper <R > decodeHelper = new DecodeHelper <>();
46
53
private final List <Throwable > throwables = new ArrayList <>();
@@ -49,6 +56,7 @@ class DecodeJob<R>
49
56
private final Pools .Pool <DecodeJob <?>> pool ;
50
57
private final DeferredEncodeManager <?> deferredEncodeManager = new DeferredEncodeManager <>();
51
58
private final ReleaseManager releaseManager = new ReleaseManager ();
59
+ private final int threadPriority = Process .getThreadPriority (Process .myTid ());
52
60
53
61
private GlideContext glideContext ;
54
62
private Key signature ;
@@ -65,6 +73,7 @@ class DecodeJob<R>
65
73
private long startFetchTime ;
66
74
private boolean onlyRetrieveFromCache ;
67
75
private Object model ;
76
+ private Supplier <Integer > glideThreadPriorityOverride ;
68
77
69
78
private Thread currentThread ;
70
79
private Key currentSourceKey ;
@@ -129,6 +138,7 @@ DecodeJob<R> init(
129
138
this .order = order ;
130
139
this .runReason = RunReason .INITIALIZE ;
131
140
this .model = model ;
141
+ this .glideThreadPriorityOverride = options .get (GLIDE_THREAD_PRIORITY );
132
142
return this ;
133
143
}
134
144
@@ -326,7 +336,17 @@ private void runGenerators() {
326
336
// onDataFetcherReady.
327
337
}
328
338
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
+
329
348
private void notifyFailed () {
349
+ restoreThreadPriority ();
330
350
setNotifiedOrThrow ();
331
351
GlideException e = new GlideException ("Failed to load resource" , new ArrayList <>(throwables ));
332
352
callback .onLoadFailed (e );
@@ -335,6 +355,7 @@ private void notifyFailed() {
335
355
336
356
private void notifyComplete (
337
357
Resource <R > resource , DataSource dataSource , boolean isLoadedFromAlternateCacheKey ) {
358
+ restoreThreadPriority ();
338
359
setNotifiedOrThrow ();
339
360
callback .onResourceReady (resource , dataSource , isLoadedFromAlternateCacheKey );
340
361
}
@@ -429,6 +450,17 @@ private void decodeFromRetrievedData() {
429
450
+ ", fetcher: "
430
451
+ currentFetcher );
431
452
}
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
+ }
432
464
Resource <R > resource = null ;
433
465
try {
434
466
resource = decodeFromData (currentFetcher , currentData , currentDataSource );
0 commit comments