Fresco源码解析 - 创建一个ImagePipeline(一)

本文涉及的产品
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
简介: 在Fresco源码解析 - 初始化过程分析章节中, 我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类。


Fresco源码解析 初始化过程分析章节中,

我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类。

 

ImagePipelineFactory.initialize(context);


会创建一个所有参数都使用默认值的ImagePipelineConfig来初始化ImagePipeline

 

ImagePipelineFactory.initialize(imagePipelineConfig)会首先用 imagePipelineConfig创建一个ImagePipelineFactory的实例 - sInstance

 

sInstance = new ImagePipelineFactory(imagePipelineConfig);


然后,初始化Drawee时,在PipelineDraweeControllerBuilderSupplier的构造方法中通过 ImagePipelineFactory.getInstance()获取这个实例。

 

Fresco.java

 

private static void initializeDrawee(Context context) {
  sDraweeControllerBuilderSupplier = new PipelineDraweeControllerBuilderSupplier(context);
  SimpleDraweeView.initialize(sDraweeControllerBuilderSupplier);
} 
PipelineDraweeControllerBuilderSupplier.java
 
public PipelineDraweeControllerBuilderSupplier(Context context) {
  this(context, ImagePipelineFactory.getInstance());
}
 
public PipelineDraweeControllerBuilderSupplier(
    Context context,
    ImagePipelineFactory imagePipelineFactory) {
  this(context, imagePipelineFactory, null);
} 





PipelineDraweeControllerBuilderSupplier还有一个构造方法,就是 this(context, imagePipelineFactory, null)调用的构造方法。

 

public PipelineDraweeControllerBuilderSupplier(
    Context context,
    ImagePipelineFactory imagePipelineFactory,
    Set<ControllerListener> boundControllerListeners) {
  mContext = context;
  mImagePipeline = imagePipelineFactory.getImagePipeline();
  mPipelineDraweeControllerFactory = new PipelineDraweeControllerFactory(
      context.getResources(),
      DeferredReleaser.getInstance(),
      imagePipelineFactory.getAnimatedDrawableFactory(),
      UiThreadImmediateExecutorService.getInstance());
  mBoundControllerListeners = boundControllerListeners;
} 





其中,mImagePipeline = imagePipelineFactory.getImagePipeline()用于获取ImagePipeline的实例。

 

ImagePipelineFactory.java

 

public ImagePipeline getImagePipeline() {
  if (mImagePipeline == null) {
    mImagePipeline =
        new ImagePipeline(
            getProducerSequenceFactory(),
            mConfig.getRequestListeners(),
            mConfig.getIsPrefetchEnabledSupplier(),
            getBitmapMemoryCache(),
            getEncodedMemoryCache(),
            mConfig.getCacheKeyFactory());
  }
  return mImagePipeline;
}



可以看出mImagePipeline是一个单例,构造ImagePipeline时用到的mConfig就是本片最开始讲到的 ImagePipelineConfig imagePipelineConfig

 

经过这个过程,一个ImagePipeline就被创建好了,下面我们具体解析一下ImagePipeline的每个参数。

 

因为ImagePipelineFactoryImagePipelineConfig来创建一个ImagePipeline,我们首先分析一下ImagePipelineConfig的源码。

 

public class ImagePipelineConfig {
  private final Supplier<MemoryCacheParams> mBitmapMemoryCacheParamsSupplier;
  private final CacheKeyFactory mCacheKeyFactory;
  private final Context mContext;
  private final Supplier<MemoryCacheParams> mEncodedMemoryCacheParamsSupplier;
  private final ExecutorSupplier mExecutorSupplier;
  private final ImageCacheStatsTracker mImageCacheStatsTracker;
  private final AnimatedDrawableUtil mAnimatedDrawableUtil;
  private final AnimatedImageFactory mAnimatedImageFactory;
  private final ImageDecoder mImageDecoder;
  private final Supplier<Boolean> mIsPrefetchEnabledSupplier;
  private final DiskCacheConfig mMainDiskCacheConfig;
  private final MemoryTrimmableRegistry mMemoryTrimmableRegistry;
  private final NetworkFetcher mNetworkFetcher;
  private final PoolFactory mPoolFactory;
  private final ProgressiveJpegConfig mProgressiveJpegConfig;
  private final Set<RequestListener> mRequestListeners;
  private final boolean mResizeAndRotateEnabledForNetwork;
  private final DiskCacheConfig mSmallImageDiskCacheConfig;
  private final PlatformBitmapFactory mPlatformBitmapFactory;
 
  // other methods
}


 

 

上图可以看出,获取图像的第一站是Memeory Cache,然后是Disk Cache,最后是Network,而MemoryDisk都是缓存在本地的数据,MemoryCacheParams就用于表示它们的缓存策略。

 

MemoryCacheParams.java

 

 /**
   * Pass arguments to control the cache's behavior in the constructor.
   *
   * @param maxCacheSize The maximum size of the cache, in bytes.
   * @param maxCacheEntries The maximum number of items that can live in the cache.
   * @param maxEvictionQueueSize The eviction queue is an area of memory that stores items ready
   *                             for eviction but have not yet been deleted. This is the maximum
   *                             size of that queue in bytes.
   * @param maxEvictionQueueEntries The maximum number of entries in the eviction queue.
   * @param maxCacheEntrySize The maximum size of a single cache entry.
   */
  public MemoryCacheParams(
      int maxCacheSize,
      int maxCacheEntries,
      int maxEvictionQueueSize,
      int maxEvictionQueueEntries,
      int maxCacheEntrySize) {
    this.maxCacheSize = maxCacheSize;
    this.maxCacheEntries = maxCacheEntries;
    this.maxEvictionQueueSize = maxEvictionQueueSize;
    this.maxEvictionQueueEntries = maxEvictionQueueEntries;
    this.maxCacheEntrySize = maxCacheEntrySize;
  }


 

关于每个参数的作用,注释已经写得很清楚,不再赘述。

 

CacheKeyFactory会为ImageRequest创建一个索引 - CacheKey

 

/**
 * Factory methods for creating cache keys for the pipeline.
 */
public interface CacheKeyFactory {
 
  /**
   * @return {@link CacheKey} for doing bitmap cache lookups in the pipeline.
   */
  public CacheKey getBitmapCacheKey(ImageRequest request);
 
  /**
   * @return {@link CacheKey} for doing encoded image lookups in the pipeline.
   */
  public CacheKey getEncodedCacheKey(ImageRequest request);
 
  /**
   * @return a {@link String} that unambiguously indicates the source of the image.
   */
  public Uri getCacheKeySourceUri(Uri sourceUri);
}





ExecutorSupplier会根据ImagePipeline的使用场景获取不同的Executor

 

public interface ExecutorSupplier {
 
  /** Executor used to do all disk reads, whether for disk cache or local files. */
  Executor forLocalStorageRead();
 
  /** Executor used to do all disk writes, whether for disk cache or local files. */
  Executor forLocalStorageWrite();
 
  /** Executor used for all decodes. */
  Executor forDecode();
 
  /** Executor used for all image transformations, such as transcoding, resizing, and rotating. */
  Executor forTransform();
 
  /** Executor used for background operations, such as postprocessing. */
  Executor forBackground();
}




ImageCacheStatsTracker 作为 Cache 埋点工具,可以统计Cache的各种操作数据。

 

public interface ImageCacheStatsTracker {
 
  /** Called whenever decoded images are put into the bitmap cache. */
  public void onBitmapCachePut();
 
  /** Called on a bitmap cache hit. */
  public void onBitmapCacheHit();
 
  /** Called on a bitmap cache miss. */
  public void onBitmapCacheMiss();
 
  /** Called whenever encoded images are put into the encoded memory cache. */
  public void onMemoryCachePut();
 
  /** Called on an encoded memory cache hit. */
  public void onMemoryCacheHit();
 
  /** Called on an encoded memory cache hit. */
  public void onMemoryCacheMiss();
 
  /**
   * Called on an staging area hit.
   *
   * <p>The staging area stores encoded images. It gets the images before they are written
   * to disk cache.
   */
  public void onStagingAreaHit();
 
  /** Called on a staging area miss hit. */
  public void onStagingAreaMiss();
 
  /** Called on a disk cache hit. */
  public void onDiskCacheHit();
 
  /** Called on a disk cache miss. */
  public void onDiskCacheMiss();
 
  /** Called if an exception is thrown on a disk cache read. */
  public void onDiskCacheGetFail();
 
  /**
   * Registers a bitmap cache with this tracker.
   *
   * <p>Use this method if you need access to the cache itself to compile your stats.
   */
  public void registerBitmapMemoryCache(CountingMemoryCache<?, ?> bitmapMemoryCache);
 
  /**
   * Registers an encoded memory cache with this tracker.
   *
   * <p>Use this method if you need access to the cache itself to compile your stats.
   */
  public void registerEncodedMemoryCache(CountingMemoryCache<?, ?> encodedMemoryCache);
}

剩下的几个参数与 Drawable 关联比较大,我们下一篇再分析。 

相关文章
|
10天前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
39 2
|
11天前
|
存储 安全 Linux
Golang的GMP调度模型与源码解析
【11月更文挑战第11天】GMP 调度模型是 Go 语言运行时系统的核心部分,用于高效管理和调度大量协程(goroutine)。它通过少量的操作系统线程(M)和逻辑处理器(P)来调度大量的轻量级协程(G),从而实现高性能的并发处理。GMP 模型通过本地队列和全局队列来减少锁竞争,提高调度效率。在 Go 源码中,`runtime.h` 文件定义了关键数据结构,`schedule()` 和 `findrunnable()` 函数实现了核心调度逻辑。通过深入研究 GMP 模型,可以更好地理解 Go 语言的并发机制。
|
23天前
|
消息中间件 缓存 安全
Future与FutureTask源码解析,接口阻塞问题及解决方案
【11月更文挑战第5天】在Java开发中,多线程编程是提高系统并发性能和资源利用率的重要手段。然而,多线程编程也带来了诸如线程安全、死锁、接口阻塞等一系列复杂问题。本文将深度剖析多线程优化技巧、Future与FutureTask的源码、接口阻塞问题及解决方案,并通过具体业务场景和Java代码示例进行实战演示。
40 3
|
1月前
|
存储
让星星⭐月亮告诉你,HashMap的put方法源码解析及其中两种会触发扩容的场景(足够详尽,有问题欢迎指正~)
`HashMap`的`put`方法通过调用`putVal`实现,主要涉及两个场景下的扩容操作:1. 初始化时,链表数组的初始容量设为16,阈值设为12;2. 当存储的元素个数超过阈值时,链表数组的容量和阈值均翻倍。`putVal`方法处理键值对的插入,包括链表和红黑树的转换,确保高效的数据存取。
56 5
|
1月前
|
Java Spring
Spring底层架构源码解析(三)
Spring底层架构源码解析(三)
113 5
|
1月前
|
XML Java 数据格式
Spring底层架构源码解析(二)
Spring底层架构源码解析(二)
|
1月前
|
算法 Java 程序员
Map - TreeSet & TreeMap 源码解析
Map - TreeSet & TreeMap 源码解析
34 0
|
1月前
|
缓存 Java 程序员
Map - LinkedHashSet&Map源码解析
Map - LinkedHashSet&Map源码解析
70 0
|
1月前
|
算法 Java 容器
Map - HashSet & HashMap 源码解析
Map - HashSet & HashMap 源码解析
57 0
|
1月前
|
存储 Java C++
Collection-PriorityQueue源码解析
Collection-PriorityQueue源码解析
62 0
下一篇
无影云桌面