aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/engine/ResourceRunner.java
blob: d0323b8eef54f78ece93ba782be618f771e3ac68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.bumptech.glide.load.engine;

import android.os.SystemClock;
import android.util.Log;
import com.bumptech.glide.load.CacheLoader;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.executor.Prioritized;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;

import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 *
 * @param <Z> The type of the resource that will be decoded.
 * @param <R> the type of the resource the decoded resource will be transcoded to.
 */
public class ResourceRunner<Z, R> implements Runnable, Prioritized {
    private static final String TAG = "ResourceRunner";

    private final EngineKey key;
    private final Transformation<Z> transformation;
    private final ResourceTranscoder<Z, R> transcoder;
    private final SourceResourceRunner sourceRunner;
    private final EngineJob job;
    private final Priority priority;
    private final ResourceDecoder<InputStream, Z> cacheDecoder;
    private final int width;
    private final int height;
    private final CacheLoader cacheLoader;
    private final ExecutorService diskCacheService;
    private final ExecutorService resizeService;
    private volatile Future<?> future;
    private volatile boolean isCancelled;

    public ResourceRunner(EngineKey key, int width, int height, CacheLoader cacheLoader,
            ResourceDecoder<InputStream, Z> cacheDecoder, Transformation<Z> transformation,
            ResourceTranscoder<Z, R> transcoder, SourceResourceRunner sourceRunner, ExecutorService diskCacheService,
            ExecutorService resizeService, EngineJob job, Priority priority) {
        this.key = key;
        this.width = width;
        this.height = height;
        this.cacheLoader = cacheLoader;
        this.cacheDecoder = cacheDecoder;
        this.transformation = transformation;
        this.transcoder = transcoder;
        this.sourceRunner = sourceRunner;
        this.diskCacheService = diskCacheService;
        this.resizeService = resizeService;
        this.job = job;
        this.priority = priority;
    }

    public EngineJob getJob() {
        return job;
    }

    public void cancel() {
        isCancelled = true;
        if (future != null) {
            future.cancel(false);
        }
        sourceRunner.cancel();
    }

    public void queue() {
        future = diskCacheService.submit(this);
    }

    @Override
    public void run() {
        if (isCancelled) {
            return;
        }

        long start = SystemClock.currentThreadTimeMillis();
        Resource<Z> fromCache = cacheLoader.load(key, cacheDecoder, width, height);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "loaded from disk cache in " + (SystemClock.currentThreadTimeMillis() - start));
        }
        if (fromCache != null) {
            Resource<Z> transformed = transformation.transform(fromCache, width, height);
            if (transformed != fromCache) {
                fromCache.recycle();
            }
            Resource<R> transcoded = transcoder.transcode(transformed);
            job.onResourceReady(transcoded);
        } else {
            future = resizeService.submit(sourceRunner);
        }
    }

    @Override
    public int getPriority() {
        return priority.ordinal();
    }
}