Fresco源码解析

解码策略

5.0以上版本使用ArtDecoder,4.4以下使用GingerbreadPurgeableDecoder,4.4到5.0之间使用KitKatPurgeableDecoder。(GingerbreadPurgeableDecoder与KitKatPurgeableDecoder为DalvikPurgeableDecoder子类)

  public static PlatformDecoder buildPlatformDecoder(
      PoolFactory poolFactory,
      boolean directWebpDirectDecodingEnabled) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      int maxNumThreads = poolFactory.getFlexByteArrayPoolMaxNumThreads();
      return new ArtDecoder(
          poolFactory.getBitmapPool(),
          maxNumThreads,
          new Pools.SynchronizedPool<>(maxNumThreads));
    } else {
      if (directWebpDirectDecodingEnabled
          && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return new GingerbreadPurgeableDecoder();
      } else {
        return new KitKatPurgeableDecoder(poolFactory.getFlexByteArrayPool());
      }
    }
  }

内存简述

基本原理参照google BITMAP内存管理

以下为fresco内存池获取内存策略

public V get(int size) {
    ...
    //依据不同类型Bucket池获取Bucket大小
    int bucketedSize = getBucketedSize(size);
    int sizeInBytes = -1;
    synchronized (this) {
     //获取Bucket池中符合需要bucketSize的Bucket
      Bucket<V> bucket = getBucket(bucketedSize);
      if (bucket != null) {
        //从Bucket池中找到可以复用的内存返回使用
        V value = bucket.get();
        ...
        return value;
        }
        // fall through
      }
    ...
    V value = null;
    try {
      ...
      //如果Bucket池中不存在可复用的内存,则申请这段内存
      value = alloc(bucketedSize);
    }...
    ...
    return value;
  }
  • 5.0及以上系统使用ArtDecoder,利用系统inBitmap方式重用内存。
    KITKAT版本之后,只要创建的Bitmap总字节数大于需要的字节数,则可用于传递给inBitmap重用内存。

    解码流程

    protected CloseableReference<Bitmap> decodeStaticImageFromStream(
         InputStream inputStream, BitmapFactory.Options options, @Nullable Rect regionToDecode) {
       ...
       //从Bitmap内存池中获取可用Bitmap内存
       final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes);
       ...
       options.inBitmap = bitmapToReuse;
       ...
       try {
        ...
        //解码流获取对应Bitmap存入设置到inBitmap的内存中
         if (decodedBitmap == null) {
           decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
         }
       } ...
       return CloseableReference.of(decodedBitmap, mBitmapPool);
     }
    

    申请Bitmap内存空间方法

    @Override
    protected Bitmap alloc(int size) {
    return Bitmap.createBitmap(
        1,
        (int) Math.ceil(size / (double) BitmapUtil.RGB_565_BYTES_PER_PIXEL),
        Bitmap.Config.RGB_565);
    }
    
  • 5.0以下系统使用DalvikPurgeableDecoder,利用Option的inPurgeable参数,当inPurgeable为true,会将图片解码数据存放在Ashmem内存中。Ashmem在unpin之前,将不会被Android系统回收,在unpin之后,系统内存不足时,将会被系统回收。KitKatPurgeableDecoder与GingerbreadPurgeableDecoder最大的不同之处在于源编码数据的存放位置不同。

    GingerbreadPurgeableDecoder源数据存放于inPurgeable如下可以看到,当bitmap大于32K的时候,会将解码数据存放于ashmem中。

    BitmapFactory.cpp
    156  static SkPixelRef* installPixelRef(SkBitmap* bitmap, SkStream* stream,
    157                                   int sampleSize, bool ditherImage) {
    158    SkImageRef* pr;
    159    // only use ashmem for large images, since mmaps come at a price
    160    if (bitmap->getSize() >= 32 * 1024) {
    161        pr = new SkImageRef_ashmem(stream, bitmap->config(), sampleSize);
    162    } else {
    163        pr = new SkImageRef_GlobalPool(stream, bitmap->config(), sampleSize);
    164    }
    165    pr->setDitherImage(ditherImage);
    166    bitmap->setPixelRef(pr)->unref();
    167    pr->isOpaque(bitmap);
    168    return pr;
    169    }
            ...
    290    if (isPurgeable) {
    291        pr = installPixelRef(bitmap, stream, sampleSize, doDither);
    292    }
        
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2021期待与你一起共事,点击查看岗位[https://www.jianshu.com/p/6f4d67fa406...
    闲庭阅读 16,947评论 0 75
  • BitmapFactory.options BitmapFactory.Options类是BitmapFactor...
    Showdy阅读 14,361评论 1 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,188评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,921评论 0 17
  • 现在 2017 07 17 23点左右 老哥喝醉回来的 说出我的身世 我不是亲生的 我觉得自己好可怕 不吃惊 也不...
    顾行星阅读 3,961评论 6 0

友情链接更多精彩内容