Search in sources :

Example 16 with Key

use of com.bumptech.glide.load.Key in project Rocket by mozilla-tw.

the class MultiModelLoader method buildLoadData.

@Override
public LoadData<Data> buildLoadData(Model model, int width, int height, Options options) {
    Key sourceKey = null;
    int size = modelLoaders.size();
    List<DataFetcher<Data>> fetchers = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        ModelLoader<Model, Data> modelLoader = modelLoaders.get(i);
        if (modelLoader.handles(model)) {
            LoadData<Data> loadData = modelLoader.buildLoadData(model, width, height, options);
            if (loadData != null) {
                sourceKey = loadData.sourceKey;
                fetchers.add(loadData.fetcher);
            }
        }
    }
    return !fetchers.isEmpty() ? new LoadData<>(sourceKey, new MultiFetcher<>(fetchers, exceptionListPool)) : null;
}
Also used : ArrayList(java.util.ArrayList) DataFetcher(com.bumptech.glide.load.data.DataFetcher) Key(com.bumptech.glide.load.Key)

Example 17 with Key

use of com.bumptech.glide.load.Key in project Rocket by mozilla-tw.

the class ResourceCacheGenerator method startNext.

@Override
public boolean startNext() {
    List<Key> sourceIds = helper.getCacheKeys();
    if (sourceIds.isEmpty()) {
        return false;
    }
    List<Class<?>> resourceClasses = helper.getRegisteredResourceClasses();
    while (modelLoaders == null || !hasNextModelLoader()) {
        resourceClassIndex++;
        if (resourceClassIndex >= resourceClasses.size()) {
            sourceIdIndex++;
            if (sourceIdIndex >= sourceIds.size()) {
                return false;
            }
            resourceClassIndex = 0;
        }
        Key sourceId = sourceIds.get(sourceIdIndex);
        Class<?> resourceClass = resourceClasses.get(resourceClassIndex);
        Transformation<?> transformation = helper.getTransformation(resourceClass);
        currentKey = new ResourceCacheKey(sourceId, helper.getSignature(), helper.getWidth(), helper.getHeight(), transformation, resourceClass, helper.getOptions());
        cacheFile = helper.getDiskCache().get(currentKey);
        if (cacheFile != null) {
            this.sourceKey = sourceId;
            modelLoaders = helper.getModelLoaders(cacheFile);
            modelLoaderIndex = 0;
        }
    }
    loadData = null;
    boolean started = false;
    while (!started && hasNextModelLoader()) {
        ModelLoader<File, ?> modelLoader = modelLoaders.get(modelLoaderIndex++);
        loadData = modelLoader.buildLoadData(cacheFile, helper.getWidth(), helper.getHeight(), helper.getOptions());
        if (loadData != null && helper.hasLoadPath(loadData.fetcher.getDataClass())) {
            started = true;
            loadData.fetcher.loadData(helper.getPriority(), this);
        }
    }
    return started;
}
Also used : File(java.io.File) Key(com.bumptech.glide.load.Key)

Example 18 with Key

use of com.bumptech.glide.load.Key in project glide by bumptech.

the class DecodeJob method onResourceDecoded.

@Synthetic
@NonNull
<Z> Resource<Z> onResourceDecoded(DataSource dataSource, @NonNull Resource<Z> decoded) {
    @SuppressWarnings("unchecked") Class<Z> resourceSubClass = (Class<Z>) decoded.get().getClass();
    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;
        switch(encodeStrategy) {
            case SOURCE:
                key = new DataCacheKey(currentSourceKey, signature);
                break;
            case TRANSFORMED:
                key = new ResourceCacheKey(decodeHelper.getArrayPool(), currentSourceKey, signature, width, height, appliedTransformation, resourceSubClass, options);
                break;
            default:
                throw new IllegalArgumentException("Unknown strategy: " + encodeStrategy);
        }
        LockedResource<Z> lockedResult = LockedResource.obtain(transformed);
        deferredEncodeManager.init(key, encoder, lockedResult);
        result = lockedResult;
    }
    return result;
}
Also used : EncodeStrategy(com.bumptech.glide.load.EncodeStrategy) Key(com.bumptech.glide.load.Key) Synthetic(com.bumptech.glide.util.Synthetic) NonNull(android.support.annotation.NonNull)

Example 19 with Key

use of com.bumptech.glide.load.Key in project glide by bumptech.

the class BitmapPreFillRunner method allocate.

/**
 * Attempts to allocate {@link android.graphics.Bitmap}s and returns {@code true} if there are
 * more {@link android.graphics.Bitmap}s to allocate and {@code false} otherwise.
 */
@VisibleForTesting
boolean allocate() {
    long start = clock.now();
    while (!toPrefill.isEmpty() && !isGcDetected(start)) {
        PreFillType toAllocate = toPrefill.remove();
        final Bitmap bitmap;
        if (!seenTypes.contains(toAllocate)) {
            seenTypes.add(toAllocate);
            bitmap = bitmapPool.getDirty(toAllocate.getWidth(), toAllocate.getHeight(), toAllocate.getConfig());
        } else {
            bitmap = Bitmap.createBitmap(toAllocate.getWidth(), toAllocate.getHeight(), toAllocate.getConfig());
        }
        // Order matters here! If the Bitmap is too large or the BitmapPool is too full, it may be
        // recycled after the call to bitmapPool#put below.
        int bitmapSize = Util.getBitmapByteSize(bitmap);
        // not empty so that we use all available space.
        if (getFreeMemoryCacheBytes() >= bitmapSize) {
            // We could probably make UniqueKey just always return false from equals,
            // but the allocation of the Key is not nearly as expensive as the allocation of the Bitmap,
            // so it's probably not worth it.
            @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") Key uniqueKey = new UniqueKey();
            memoryCache.put(uniqueKey, BitmapResource.obtain(bitmap, bitmapPool));
        } else {
            bitmapPool.put(bitmap);
        }
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "allocated [" + toAllocate.getWidth() + "x" + toAllocate.getHeight() + "] " + toAllocate.getConfig() + " size: " + bitmapSize);
        }
    }
    return !isCancelled && !toPrefill.isEmpty();
}
Also used : Bitmap(android.graphics.Bitmap) Key(com.bumptech.glide.load.Key) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 20 with Key

use of com.bumptech.glide.load.Key in project glide by bumptech.

the class RecyclerAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ListViewHolder viewHolder, int position) {
    MediaStoreData current = data.get(position);
    Key signature = new MediaStoreSignature(current.mimeType, current.dateModified, current.orientation);
    requestBuilder.clone().signature(signature).load(current.uri).into(viewHolder.image);
}
Also used : MediaStoreSignature(com.bumptech.glide.signature.MediaStoreSignature) Key(com.bumptech.glide.load.Key)

Aggregations

Key (com.bumptech.glide.load.Key)24 Test (org.junit.Test)13 ResourceWeakReference (com.bumptech.glide.load.engine.ActiveResources.ResourceWeakReference)7 File (java.io.File)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Context (android.content.Context)2 NonNull (android.support.annotation.NonNull)2 DataFetcher (com.bumptech.glide.load.data.DataFetcher)2 ArrayList (java.util.ArrayList)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Bitmap (android.graphics.Bitmap)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 EncodeStrategy (com.bumptech.glide.load.EncodeStrategy)1 Resource (com.bumptech.glide.load.engine.Resource)1 ResourceRemovedListener (com.bumptech.glide.load.engine.cache.MemoryCache.ResourceRemovedListener)1 MediaStoreSignature (com.bumptech.glide.signature.MediaStoreSignature)1 Util.anyResource (com.bumptech.glide.tests.Util.anyResource)1 Util.mockResource (com.bumptech.glide.tests.Util.mockResource)1 LruCache (com.bumptech.glide.util.LruCache)1 Synthetic (com.bumptech.glide.util.Synthetic)1