aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/engine/SourceResourceRunner.java
blob: 03decf8b2f13bef8a3f0003f87a9cb052d01f51d (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.bumptech.glide.load.engine;

import android.os.SystemClock;
import android.util.Log;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.CacheLoader;
import com.bumptech.glide.load.Encoder;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.executor.Prioritized;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.request.ResourceCallback;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 *
 * @param <T> The type of the data the resource will be decoded from.
 * @param <Z> The type of the resource that will be decoded.
 * @param <R> The type of the resource that will be transcoded to from the decoded resource.
 */
public class SourceResourceRunner<T, Z, R> implements Runnable, Prioritized {
    private static final WriterFactory DEFAULT_WRITER_FACTORY = new DefaultWriterFactory();

    private static final String TAG = "SourceRunner";
    private final EngineKey key;
    private final int width;
    private final int height;
    private final CacheLoader cacheLoader;
    private final ResourceDecoder<File, Z> cacheDecoder;
    private final DataFetcher<T> fetcher;
    private final boolean cacheSource;
    private final Encoder<T> sourceEncoder;
    private final ResourceDecoder<T, Z> decoder;
    private final Transformation<Z> transformation;
    private final ResourceEncoder<Z> encoder;
    private final ResourceTranscoder<Z, R> transcoder;
    private final DiskCache diskCache;
    private final Priority priority;
    private final ResourceCallback cb;
    private WriterFactory writerFactory;

    private volatile boolean isCancelled;

    public SourceResourceRunner(EngineKey key,
                         int width,
                         int height,
                         CacheLoader cacheLoader,
                         ResourceDecoder<File, Z> cacheDecoder,
                         DataFetcher<T> dataFetcher,
                         boolean cacheSource,
                         Encoder<T> sourceEncoder,
                         ResourceDecoder<T, Z> decoder,
                         Transformation<Z> transformation,
                         ResourceEncoder<Z> encoder,
                         ResourceTranscoder<Z, R> transcoder,
                         DiskCache diskCache,
                         Priority priority,
                         ResourceCallback cb) {
        this(key, width, height, cacheLoader, cacheDecoder, dataFetcher, cacheSource, sourceEncoder, decoder,
                transformation, encoder, transcoder, diskCache, priority, cb, DEFAULT_WRITER_FACTORY);
    }

    SourceResourceRunner(EngineKey key,
                         int width,
                         int height,
                         CacheLoader cacheLoader,
                         ResourceDecoder<File, Z> cacheDecoder,
                         DataFetcher<T> dataFetcher,
                         boolean cacheSource,
                         Encoder<T> sourceEncoder,
                         ResourceDecoder<T, Z> decoder,
                         Transformation<Z> transformation,
                         ResourceEncoder<Z> encoder,
                         ResourceTranscoder<Z, R> transcoder,
                         DiskCache diskCache,
                         Priority priority,
                         ResourceCallback cb,
                         WriterFactory writerFactory) {
        this.key = key;
        this.width = width;
        this.height = height;
        this.cacheLoader = cacheLoader;
        this.cacheDecoder = cacheDecoder;
        this.fetcher = dataFetcher;
        this.cacheSource = cacheSource;
        this.sourceEncoder = sourceEncoder;
        this.decoder = decoder;
        this.transformation = transformation;
        this.encoder = encoder;
        this.transcoder = transcoder;
        this.diskCache = diskCache;
        this.priority = priority;
        this.cb = cb;
        this.writerFactory = writerFactory;
    }

    public void cancel() {
        isCancelled = true;
        if (fetcher != null) {
            fetcher.cancel();
        }
    }

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

        try {
            long start = SystemClock.currentThreadTimeMillis();
            Resource<Z> decoded = cacheLoader.load(key.getOriginalKey(), cacheDecoder, width, height);

            if (decoded == null) {
                decoded = decodeFromSource();
                if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.v(TAG, "Decoded from source in " + (SystemClock.currentThreadTimeMillis() - start) + " cache");
                    start = SystemClock.currentThreadTimeMillis();
                }
            }

            Resource<Z> result = null;

            if (decoded != null) {
                Resource<Z> transformed = transformation.transform(decoded, width, height);
                if (decoded != transformed) {
                    decoded.recycle();
                }
                result = transformed;
                if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.v(TAG, "transformed in " + (SystemClock.currentThreadTimeMillis() - start));
                }
            }

            if (result != null) {
                diskCache.put(key, writerFactory.build(encoder, result));
                start = SystemClock.currentThreadTimeMillis();
                Resource<R> transcoded = transcoder.transcode(result);
                if (Log.isLoggable(TAG, Log.VERBOSE)) {
                    Log.d(TAG, "transcoded in " + (SystemClock.currentThreadTimeMillis() - start));
                }
                cb.onResourceReady(transcoded);
            } else {
                cb.onException(null);
            }

        } catch (Exception e) {
            cb.onException(e);
        }
    }

    private Resource<Z> encodeSourceAndDecodeFromCache(final T data) {
        diskCache.put(key.getOriginalKey(), writerFactory.build(sourceEncoder, data));
        return cacheLoader.load(key.getOriginalKey(), cacheDecoder, width, height);
    }

    private Resource<Z> decodeFromSource() throws Exception {
        try {
            final T data = fetcher.loadData(priority);
            if (data != null) {
                if (cacheSource) {
                    return encodeSourceAndDecodeFromCache(data);
                } else {
                    return decoder.decode(data, width, height);
                }
            }
        } finally {
            fetcher.cleanup();
        }

        return null;
    }

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

    private static class DefaultWriterFactory implements WriterFactory {

        @Override
        public <T> SourceWriter<T> build(Encoder<T> encoder, T data) {
            return new SourceWriter<T>(encoder, data);
        }
    }

    interface WriterFactory {
        public <T> SourceWriter<T> build(Encoder<T> encoder, T data);
    }

    static class SourceWriter<T> implements DiskCache.Writer {

        private final Encoder<T> encoder;
        private final T data;

        public SourceWriter(Encoder<T> encoder, T data) {
            this.encoder = encoder;
            this.data = data;
        }

        @Override
        public boolean write(File file) {
            long start = SystemClock.currentThreadTimeMillis();
            boolean success = false;
            OutputStream os = null;
            try {
                os = new FileOutputStream(file);
                success = encoder.encode(data, os);
            } catch (FileNotFoundException e) {
                if (Log.isLoggable(TAG, Log.DEBUG)) {
                    Log.d(TAG, "Failed to find file to write to disk cache", e);
                }
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        // Do nothing.
                    }
                }
            }
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "wrote to disk cache in " + (SystemClock.currentThreadTimeMillis() - start));
            }
            return success;
        }
    }
}