All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bumptech.glide.load.engine.DecodePath Maven / Gradle / Ivy

Go to download

A fast and efficient image loading library for Android focused on smooth scrolling.

There is a newer version: 5.0.0-rc01
Show newest version
package com.bumptech.glide.load.engine;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.util.Pools.Pool;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.data.DataRewinder;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.util.Preconditions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Attempts to decode and transcode resource type from a given data type.
 *
 * @param  The type of data ResourceType that will be decoded from.
 * @param  The type of intermediate resource that will be decoded.
 * @param  The final type of resource that will be transcoded from ResourceType and
 *     returned to the caller.
 */
public class DecodePath {
  private static final String TAG = "DecodePath";
  private final Class dataClass;
  private final List> decoders;
  private final ResourceTranscoder transcoder;
  private final Pool> listPool;
  private final String failureMessage;

  public DecodePath(
      Class dataClass,
      Class resourceClass,
      Class transcodeClass,
      List> decoders,
      ResourceTranscoder transcoder,
      Pool> listPool) {
    this.dataClass = dataClass;
    this.decoders = decoders;
    this.transcoder = transcoder;
    this.listPool = listPool;
    failureMessage =
        "Failed DecodePath{"
            + dataClass.getSimpleName()
            + "->"
            + resourceClass.getSimpleName()
            + "->"
            + transcodeClass.getSimpleName()
            + "}";
  }

  public Resource decode(
      DataRewinder rewinder,
      int width,
      int height,
      @NonNull Options options,
      DecodeCallback callback)
      throws GlideException {
    Resource decoded = decodeResource(rewinder, width, height, options);
    Resource transformed = callback.onResourceDecoded(decoded);
    return transcoder.transcode(transformed, options);
  }

  @NonNull
  private Resource decodeResource(
      DataRewinder rewinder, int width, int height, @NonNull Options options)
      throws GlideException {
    List exceptions = Preconditions.checkNotNull(listPool.acquire());
    try {
      return decodeResourceWithList(rewinder, width, height, options, exceptions);
    } finally {
      listPool.release(exceptions);
    }
  }

  @NonNull
  private Resource decodeResourceWithList(
      DataRewinder rewinder,
      int width,
      int height,
      @NonNull Options options,
      List exceptions)
      throws GlideException {
    Resource result = null;
    //noinspection ForLoopReplaceableByForEach to improve perf
    for (int i = 0, size = decoders.size(); i < size; i++) {
      ResourceDecoder decoder = decoders.get(i);
      try {
        DataType data = rewinder.rewindAndGet();
        if (decoder.handles(data, options)) {
          data = rewinder.rewindAndGet();
          result = decoder.decode(data, width, height, options);
        }
        // Some decoders throw unexpectedly. If they do, we shouldn't fail the entire load path, but
        // instead log and continue. See #2406 for an example.
      } catch (IOException | RuntimeException | OutOfMemoryError e) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
          Log.v(TAG, "Failed to decode data for " + decoder, e);
        }
        exceptions.add(e);
      }

      if (result != null) {
        break;
      }
    }

    if (result == null) {
      throw new GlideException(failureMessage, new ArrayList<>(exceptions));
    }
    return result;
  }

  @Override
  public String toString() {
    return "DecodePath{"
        + " dataClass="
        + dataClass
        + ", decoders="
        + decoders
        + ", transcoder="
        + transcoder
        + '}';
  }

  interface DecodeCallback {
    @NonNull
    Resource onResourceDecoded(@NonNull Resource resource);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy