@Override public void onSizeReady(int width, int height) { if (isCancelled) { return; } width = Math.round(sizeMultiplier * width); height = Math.round(sizeMultiplier * height); ResourceDecoder<InputStream, Z> cacheDecoder = loadProvider.getCacheDecoder(); ResourceDecoder<T, Z> decoder = loadProvider.getSourceDecoder(); ResourceEncoder <Z> encoder = loadProvider.getEncoder(); ResourceTranscoder<Z, R> transcoder = loadProvider.getTranscoder(); ModelLoader<A, T> modelLoader = loadProvider.getModelLoader(); final String id = modelLoader.getId(model); final DataFetcher<T> dataFetcher = modelLoader.getResourceFetcher(model, width, height); loadedFromMemoryCache = true; loadStatus = engine.load(id, width, height, cacheDecoder, dataFetcher, decoder, transformation, encoder, transcoder, priority, isMemoryCacheable, this); loadedFromMemoryCache = resource != null; }
@SuppressWarnings("unchecked") @Nullable public synchronized <Z> ResourceEncoder<Z> get(Class<Z> resourceClass) { int size = encoders.size(); for (int i = 0; i < size; i++) { Entry<?> entry = encoders.get(i); if (entry.handles(resourceClass)) { return (ResourceEncoder<Z>) entry.encoder; } } // TODO: throw an exception here? return null; }
public <X> ResourceEncoder<X> getResultEncoder(Resource<X> resource) throws NoResultEncoderAvailableException { ResourceEncoder<X> resourceEncoder = resourceEncoderRegistry.get(resource.getResourceClass()); if (resourceEncoder != null) { return resourceEncoder; } throw new NoResultEncoderAvailableException(resource.getResourceClass()); }
public SourceResourceRunner(Key key, int width, int height, DataFetcher<T> dataFetcher, ResourceDecoder<T, Z> decoder, Transformation<Z> transformation, ResourceEncoder<Z> encoder, ResourceTranscoder<Z, R> transcoder, DiskCache diskCache, Priority priority, ResourceCallback cb) { this.key = key; this.width = width; this.height = height; this.fetcher = dataFetcher; this.decoder = decoder; this.transformation = transformation; this.encoder = encoder; this.transcoder = transcoder; this.diskCache = diskCache; this.priority = priority; this.cb = cb; }
public EngineKey(String id, int width, int height, ResourceDecoder cacheDecoder, ResourceDecoder decoder, Transformation transformation, ResourceEncoder encoder, ResourceTranscoder transcoder) { this.id = id; this.width = width; this.height = height; this.cacheDecoder = cacheDecoder; this.decoder = decoder; this.transformation = transformation; this.encoder = encoder; this.transcoder = transcoder; }
/** * * @param id A unique id for the model, dimensions, cache decoder, decoder, and encoder * @param cacheDecoder * @param fetcher * @param decoder * @param encoder * @param transcoder * @param priority * @param <T> The type of 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 from the decoded resource. */ public <T, Z, R> LoadStatus load(String id, int width, int height, ResourceDecoder<InputStream, Z> cacheDecoder, DataFetcher<T> fetcher, ResourceDecoder<T, Z> decoder, Transformation<Z> transformation, ResourceEncoder<Z> encoder, ResourceTranscoder<Z, R> transcoder, Priority priority, boolean isMemoryCacheable, ResourceCallback cb) { Key key = keyFactory.buildKey(id, width, height, cacheDecoder, decoder, transformation, encoder, transcoder); Resource cached = cache.get(key); if (cached != null) { cached.acquire(1); cb.onResourceReady(cached); return null; } ResourceRunner current = runners.get(key); if (current != null) { EngineJob job = current.getJob(); job.addCallback(cb); return new LoadStatus(cb, job); } ResourceRunner<Z, R> runner = factory.build(key, width, height, cacheDecoder, fetcher, decoder, transformation, encoder, transcoder, priority, isMemoryCacheable, this); runner.getJob().addCallback(cb); runners.put(key, runner); runner.queue(); return new LoadStatus(cb, runner.getJob()); }
@Override public <T, Z, R> ResourceRunner<Z, R> build(Key key, int width, int height, ResourceDecoder<InputStream, Z> cacheDecoder, DataFetcher<T> fetcher, ResourceDecoder<T, Z> decoder, Transformation<Z> transformation, ResourceEncoder<Z> encoder, ResourceTranscoder<Z, R> transcoder, Priority priority, boolean isMemoryCacheable, EngineJobListener listener) { EngineJob engineJob = new EngineJob(key, memoryCache, mainHandler, isMemoryCacheable, listener); SourceResourceRunner<T, Z, R> sourceRunner = new SourceResourceRunner<T, Z, R>(key, width, height, fetcher, decoder, transformation, encoder, transcoder, diskCache, priority, engineJob); return new ResourceRunner<Z, R>(key, width, height, diskCache, cacheDecoder, transcoder, sourceRunner, service, bgHandler, engineJob); }
@Override public ResourceEncoder<Z> getEncoder() { if (encoder != null) { return encoder; } else { return parent.getEncoder(); } }
public BitmapDrawableEncoder(BitmapPool bitmapPool, ResourceEncoder<Bitmap> encoder) { this.bitmapPool = bitmapPool; this.encoder = encoder; }
<Z> ResourceEncoder<Z> getResultEncoder(Resource<Z> resource) { return glideContext.getRegistry().getResultEncoder(resource); }
@Override public Resource<Z> onResourceDecoded(Resource<Z> decoded) { Class<Z> resourceSubClass = getResourceClass(decoded); Transformation<Z> appliedTransformation = null; Resource<Z> transformed = decoded; if (dataSource != DataSource.RESOURCE_DISK_CACHE) { appliedTransformation = decodeHelper.getTransformation(resourceSubClass); transformed = appliedTransformation.transform(glideContext, decoded, width, height); } // TODO: Make this the responsibility of the Transformation. if (!decoded.equals(transformed)) { decoded.recycle(); } final EncodeStrategy encodeStrategy; final ResourceEncoder<Z> encoder; if (decodeHelper.isResourceEncoderAvailable(transformed)) { encoder = decodeHelper.getResultEncoder(transformed); encodeStrategy = encoder.getEncodeStrategy(options); } else { encoder = null; encodeStrategy = EncodeStrategy.NONE; } Resource<Z> result = transformed; boolean isFromAlternateCacheKey = !decodeHelper.isSourceKey(currentSourceKey); if (diskCacheStrategy.isResourceCacheable(isFromAlternateCacheKey, dataSource, encodeStrategy)) { if (encoder == null) { throw new Registry.NoResultEncoderAvailableException(transformed.get().getClass()); } final Key key; if (encodeStrategy == EncodeStrategy.SOURCE) { key = new DataCacheKey(currentSourceKey, signature); } else if (encodeStrategy == EncodeStrategy.TRANSFORMED) { key = new ResourceCacheKey(currentSourceKey, signature, width, height, appliedTransformation, resourceSubClass, options); } else { throw new IllegalArgumentException("Unknown strategy: " + encodeStrategy); } LockedResource<Z> lockedResult = LockedResource.obtain(transformed); deferredEncodeManager.init(key, encoder, lockedResult); result = lockedResult; } return result; }
@SuppressWarnings("unchecked") <X> void init(Key key, ResourceEncoder<X> encoder, LockedResource<X> toEncode) { this.key = key; this.encoder = (ResourceEncoder<Z>) encoder; this.toEncode = (LockedResource<Z>) toEncode; }
public synchronized <Z> void add(Class<Z> resourceClass, ResourceEncoder<Z> encoder) { encoders.add(new Entry<>(resourceClass, encoder)); }
Entry(Class<T> resourceClass, ResourceEncoder<T> encoder) { this.resourceClass = resourceClass; this.encoder = encoder; }
public <TResource> Registry register(Class<TResource> resourceClass, ResourceEncoder<TResource> encoder) { resourceEncoderRegistry.add(resourceClass, encoder); return this; }
@Override public Resource<Z> onResourceDecoded(Resource<Z> decoded) { Class<Z> resourceSubClass = getResourceClass(decoded); Transformation<Z> appliedTransformation = null; Resource<Z> transformed = decoded; if (dataSource != DataSource.RESOURCE_DISK_CACHE) { appliedTransformation = decodeHelper.getTransformation(resourceSubClass); transformed = appliedTransformation.transform(glideContext, decoded, width, height); } // TODO: Make this the responsibility of the Transformation. if (!decoded.equals(transformed)) { decoded.recycle(); } final EncodeStrategy encodeStrategy; final ResourceEncoder<Z> encoder; if (decodeHelper.isResourceEncoderAvailable(transformed)) { encoder = decodeHelper.getResultEncoder(transformed); encodeStrategy = encoder.getEncodeStrategy(options); } else { encoder = null; encodeStrategy = EncodeStrategy.NONE; } Resource<Z> result = transformed; boolean isFromAlternateCacheKey = !decodeHelper.isSourceKey(currentSourceKey); if (diskCacheStrategy.isResourceCacheable(isFromAlternateCacheKey, dataSource, encodeStrategy)) { if (encoder == null) { throw new Registry.NoResultEncoderAvailableException(transformed.get().getClass()); } final Key key; if (encodeStrategy == EncodeStrategy.SOURCE) { key = new DataCacheKey(currentSourceKey, signature); } else if (encodeStrategy == EncodeStrategy.TRANSFORMED) { key = new ResourceCacheKey( decodeHelper.getArrayPool(), currentSourceKey, signature, width, height, appliedTransformation, resourceSubClass, options); } else { throw new IllegalArgumentException("Unknown strategy: " + encodeStrategy); } LockedResource<Z> lockedResult = LockedResource.obtain(transformed); deferredEncodeManager.init(key, encoder, lockedResult); result = lockedResult; } return result; }
public synchronized <Z> void append(Class<Z> resourceClass, ResourceEncoder<Z> encoder) { encoders.add(new Entry<>(resourceClass, encoder)); }