@OverridepublicsynchronizedvoidonEngineJobComplete(EngineJob<?>engineJob,Keykey,EngineResource<?>resource){// A null resource indicates that the load failed, usually due to an exception.//添加到ActiveResources中if(resource!=null&&resource.isMemoryCacheable()){activeResources.activate(key,resource);}jobs.removeIfCurrent(key,engineJob);}
@NullableprivateEngineResource<?>loadFromMemory(EngineKeykey,booleanisMemoryCacheable,longstartTime){//内存缓存不可用直接返回nullif(!isMemoryCacheable){returnnull;}EngineResource<?>active=loadFromActiveResources(key);if(active!=null){if(VERBOSE_IS_LOGGABLE){logWithTimeAndKey("Loaded resource from active resources",startTime,key);}returnactive;}EngineResource<?>cached=loadFromCache(key);if(cached!=null){if(VERBOSE_IS_LOGGABLE){logWithTimeAndKey("Loaded resource from cache",startTime,key);}returncached;}returnnull;}
privateEngineResource<?>getEngineResourceFromCache(Keykey){//从MemoryCache中移除Resource<?>cached=cache.remove(key);finalEngineResource<?>result;if(cached==null){result=null;}elseif(cachedinstanceofEngineResource){// Save an object allocation if we've cached an EngineResource (the typical case).result=(EngineResource<?>)cached;}else{result=newEngineResource<>(cached,/*isMemoryCacheable=*/true,/*isRecyclable=*/true,key,/*listener=*/this);}returnresult;}
@OverridepublicvoidonResourceRemoved(@NonNullfinalResource<?>resource){// Avoid deadlock with RequestManagers when recycling triggers recursive clear() calls.// See b/145519760.//调用ResourceRecycler的recycle方法回收资源resourceRecycler.recycle(resource,/*forceNextFrame=*/true);}
publicstaticfinalclassBuilder{@VisibleForTestingstaticfinalintMEMORY_CACHE_TARGET_SCREENS=2;/** * On Android O+, we use {@link android.graphics.Bitmap.Config#HARDWARE} for all reasonably * sized images unless we're creating thumbnails for the first time. As a result, the Bitmap * pool is much less important on O than it was on previous versions. */staticfinalintBITMAP_POOL_TARGET_SCREENS=Build.VERSION.SDK_INT<Build.VERSION_CODES.O?4:1;staticfinalfloatMAX_SIZE_MULTIPLIER=0.4f;staticfinalfloatLOW_MEMORY_MAX_SIZE_MULTIPLIER=0.33f;// 4MB.staticfinalintARRAY_POOL_SIZE_BYTES=4*1024*1024;@SyntheticfinalContextcontext;// Modifiable (non-final) for testing.@SyntheticActivityManageractivityManager;@SyntheticScreenDimensionsscreenDimensions;@SyntheticfloatmemoryCacheScreens=MEMORY_CACHE_TARGET_SCREENS;@SyntheticfloatbitmapPoolScreens=BITMAP_POOL_TARGET_SCREENS;@SyntheticfloatmaxSizeMultiplier=MAX_SIZE_MULTIPLIER;@SyntheticfloatlowMemoryMaxSizeMultiplier=LOW_MEMORY_MAX_SIZE_MULTIPLIER;@SyntheticintarrayPoolSizeBytes=ARRAY_POOL_SIZE_BYTES;}