com.facebook.common.references.ResourceReleaser Java Examples

The following examples show how to use com.facebook.common.references.ResourceReleaser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DalvikBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
public DalvikBitmapFactory(
    EmptyJpegGenerator jpegGenerator,
    SingleByteArrayPool singleByteArrayPool) {
  mJpegGenerator = jpegGenerator;
  mSingleByteArrayPool = singleByteArrayPool;
  mUnpooledBitmapsCounter = BitmapCounterProvider.get();
  mUnpooledBitmapsReleaser = new ResourceReleaser<Bitmap>() {
    @Override
    public void release(Bitmap value) {
      try {
        mUnpooledBitmapsCounter.decrease(value);
      } finally {
        value.recycle();
      }
    }
  };
}
 
Example #2
Source File: CloseableProducerToDataSourceAdapterTest.java    From fresco with MIT License 6 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  mResourceReleaser = mock(ResourceReleaser.class);
  mResultRef1 = CloseableReference.of(new Object(), mResourceReleaser);
  mResultRef2 = CloseableReference.of(new Object(), mResourceReleaser);
  mResultRef3 = CloseableReference.of(new Object(), mResourceReleaser);
  mException = mock(Exception.class);

  mDataSubscriber1 = mock(DataSubscriber.class);
  mDataSubscriber2 = mock(DataSubscriber.class);

  mSettableProducerContext = mock(SettableProducerContext.class);
  mProducer = mock(Producer.class);
  mDataSource =
      CloseableProducerToDataSourceAdapter.create(
          mProducer, mSettableProducerContext, mRequestListener);
  ArgumentCaptor<Consumer> captor = ArgumentCaptor.forClass(Consumer.class);
  verify(mRequestListener).onRequestStart(mSettableProducerContext);
  verify(mProducer).produceResults(captor.capture(), any(SettableProducerContext.class));
  mInternalConsumer = captor.getValue();

  mDataSource.subscribe(mDataSubscriber1, CallerThreadExecutor.getInstance());
}
 
Example #3
Source File: SharedByteArray.java    From fresco with MIT License 6 votes vote down vote up
public SharedByteArray(MemoryTrimmableRegistry memoryTrimmableRegistry, PoolParams params) {
  Preconditions.checkNotNull(memoryTrimmableRegistry);
  Preconditions.checkArgument(params.minBucketSize > 0);
  Preconditions.checkArgument(params.maxBucketSize >= params.minBucketSize);

  mMaxByteArraySize = params.maxBucketSize;
  mMinByteArraySize = params.minBucketSize;
  mByteArraySoftRef = new OOMSoftReference<byte[]>();
  mSemaphore = new Semaphore(1);
  mResourceReleaser =
      new ResourceReleaser<byte[]>() {
        @Override
        public void release(byte[] unused) {
          mSemaphore.release();
        }
      };

  memoryTrimmableRegistry.registerMemoryTrimmable(this);
}
 
Example #4
Source File: BitmapCounter.java    From fresco with MIT License 6 votes vote down vote up
public BitmapCounter(int maxCount, int maxSize) {
  Preconditions.checkArgument(maxCount > 0);
  Preconditions.checkArgument(maxSize > 0);
  mMaxCount = maxCount;
  mMaxSize = maxSize;
  mUnpooledBitmapsReleaser =
      new ResourceReleaser<Bitmap>() {
        @Override
        public void release(Bitmap value) {
          try {
            decrease(value);
          } finally {
            value.recycle();
          }
        }
      };
}
 
Example #5
Source File: FrescoDrawableTest.java    From fresco with MIT License 6 votes vote down vote up
@Before
public void setup() {
  mFrescoDrawable = new FrescoDrawable();
  mLatch = new CountDownLatch(1);
  mCloseableImage = new DummyCloseableImage();
  mCloseableReference =
      CloseableReference.of(
          mCloseableImage,
          new ResourceReleaser<CloseableImage>() {
            @Override
            public void release(CloseableImage value) {
              value.close();
              mLatch.countDown();
            }
          });
}
 
Example #6
Source File: CloseableStaticBitmapTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testWidthAndHeightWithInvertedOrientationImage() {
  // Reverse width and height as the inverted orienvation should put them back again
  mBitmap = Bitmap.createBitmap(HEIGHT, WIDTH, Bitmap.Config.ARGB_8888);
  ResourceReleaser<Bitmap> releaser = SimpleBitmapReleaser.getInstance();
  mCloseableStaticBitmap =
      new CloseableStaticBitmap(
          mBitmap,
          releaser,
          ImmutableQualityInfo.FULL_QUALITY,
          0,
          ExifInterface.ORIENTATION_TRANSPOSE);

  assertThat(mCloseableStaticBitmap.getWidth()).isEqualTo(WIDTH);
  assertThat(mCloseableStaticBitmap.getHeight()).isEqualTo(HEIGHT);
}
 
Example #7
Source File: CloseableStaticBitmapTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testWidthAndHeightWithRotatedImage() {
  // Reverse width and height as the rotation angle should put them back again
  mBitmap = Bitmap.createBitmap(HEIGHT, WIDTH, Bitmap.Config.ARGB_8888);
  ResourceReleaser<Bitmap> releaser = SimpleBitmapReleaser.getInstance();
  mCloseableStaticBitmap =
      new CloseableStaticBitmap(
          mBitmap,
          releaser,
          ImmutableQualityInfo.FULL_QUALITY,
          90,
          ExifInterface.ORIENTATION_ROTATE_90);

  assertThat(mCloseableStaticBitmap.getWidth()).isEqualTo(WIDTH);
  assertThat(mCloseableStaticBitmap.getHeight()).isEqualTo(HEIGHT);
}
 
Example #8
Source File: CloseableAnimatedBitmap.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a CloseableStaticBitmap.
 * @param bitmaps the bitmap frames. This list must be immutable.
 * @param durations the frame durations, This list must be immutable.
 * @param resourceReleaser ResourceReleaser to release the bitmaps to
 */
public CloseableAnimatedBitmap(
    List<Bitmap> bitmaps,
    List<Integer> durations,
    ResourceReleaser<Bitmap> resourceReleaser) {
  Preconditions.checkNotNull(bitmaps);
  Preconditions.checkState(bitmaps.size() >= 1, "Need at least 1 frame!");
  mBitmaps = Lists.newArrayList();
  mBitmapReferences = Lists.newArrayList();
  for (Bitmap bitmap : bitmaps) {
    mBitmapReferences.add(CloseableReference.of(bitmap, resourceReleaser));
    mBitmaps.add(bitmap);
  }
  mDurations = Preconditions.checkNotNull(durations);
  Preconditions.checkState(mDurations.size() == mBitmaps.size(), "Arrays length mismatch!");
}
 
Example #9
Source File: ReferenceWrappingMemoryCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private CloseableReference<V> wrapCacheReferenceIfNotNull(
    final K key,
    final @Nullable CloseableReference<V> value) {
  if (value == null) {
    return null;
  }

  return CloseableReference.of(value.get(), new ResourceReleaser<V>() {
        @Override
        public void release(V unused) {
          mCountingMemoryCache.release(key, value);
        }
      });
}
 
Example #10
Source File: KitKatPurgeableDecoderTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  mFlexByteArrayPool = mock(FlexByteArrayPool.class);

  mBitmap = MockBitmapFactory.create();
  mBitmapCounter = new BitmapCounter(MAX_BITMAP_COUNT, MAX_BITMAP_SIZE);

  mockStatic(DalvikPurgeableDecoder.class);
  when(DalvikPurgeableDecoder.getBitmapFactoryOptions(anyInt(), any(Bitmap.Config.class)))
      .thenCallRealMethod();
  when(DalvikPurgeableDecoder.endsWithEOI(any(CloseableReference.class), anyInt()))
      .thenCallRealMethod();
  mockStatic(BitmapCounterProvider.class);
  when(BitmapCounterProvider.get()).thenReturn(mBitmapCounter);

  mockStatic(BitmapFactory.class);
  when(BitmapFactory.decodeByteArray(
          any(byte[].class), anyInt(), anyInt(), any(BitmapFactory.Options.class)))
      .thenReturn(mBitmap);

  mInputBuf = new byte[LENGTH];
  PooledByteBuffer input = new TrivialPooledByteBuffer(mInputBuf, POINTER);
  mByteBufferRef = CloseableReference.of(input);
  mEncodedImage = new EncodedImage(mByteBufferRef);

  mDecodeBuf = new byte[LENGTH + 2];
  mDecodeBufRef = CloseableReference.of(mDecodeBuf, mock(ResourceReleaser.class));
  when(mFlexByteArrayPool.get(Integer.valueOf(LENGTH))).thenReturn(mDecodeBufRef);

  mockStatic(Bitmaps.class);
  mKitKatPurgeableDecoder = new KitKatPurgeableDecoder(mFlexByteArrayPool);
}
 
Example #11
Source File: GingerbreadPurgeableDecoderTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {

  mBitmap = MockBitmapFactory.create();
  mBitmapCounter = new BitmapCounter(MAX_BITMAP_COUNT, MAX_BITMAP_SIZE);

  mockStatic(DalvikPurgeableDecoder.class);
  when(DalvikPurgeableDecoder.getBitmapFactoryOptions(anyInt(), any(Bitmap.Config.class)))
      .thenCallRealMethod();
  when(DalvikPurgeableDecoder.endsWithEOI(any(CloseableReference.class), anyInt()))
      .thenCallRealMethod();
  mockStatic(BitmapCounterProvider.class);
  when(BitmapCounterProvider.get()).thenReturn(mBitmapCounter);

  mockStatic(BitmapFactory.class);
  when(BitmapFactory.decodeFileDescriptor(
          any(FileDescriptor.class), any(Rect.class), any(BitmapFactory.Options.class)))
      .thenReturn(mBitmap);

  mInputBuf = new byte[LENGTH];
  PooledByteBuffer input = new TrivialPooledByteBuffer(mInputBuf, POINTER);
  mByteBufferRef = CloseableReference.of(input);
  mEncodedImage = new EncodedImage(mByteBufferRef);

  mDecodeBuf = new byte[LENGTH + 2];
  mDecodeBufRef = CloseableReference.of(mDecodeBuf, mock(ResourceReleaser.class));

  mockStatic(Bitmaps.class);
  mGingerbreadPurgeableDecoder = new GingerbreadPurgeableDecoder();
}
 
Example #12
Source File: PooledByteArrayBufferedInputStreamTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  mResourceReleaser = mock(ResourceReleaser.class);
  final byte[] bytes = new byte[256];
  for (int i = 0; i < 256; ++i) {
    bytes[i] = (byte) i;
  }
  InputStream unbufferedStream = new ByteArrayInputStream(bytes);
  mBuffer = new byte[10];
  mPooledByteArrayBufferedInputStream =
      new PooledByteArrayBufferedInputStream(unbufferedStream, mBuffer, mResourceReleaser);
}
 
Example #13
Source File: GingerbreadBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
public GingerbreadBitmapFactory() {
  mBitmapResourceReleaser = new ResourceReleaser<Bitmap>() {
    @Override
    public void release(Bitmap value) {
      value.recycle();
    }
  };
}
 
Example #14
Source File: PooledByteArrayBufferedInputStream.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
public PooledByteArrayBufferedInputStream(
    InputStream inputStream,
    byte[] byteArray,
    ResourceReleaser<byte[]> resourceReleaser) {
  mInputStream = Preconditions.checkNotNull(inputStream);
  mByteArray = Preconditions.checkNotNull(byteArray);
  mResourceReleaser = Preconditions.checkNotNull(resourceReleaser);
  mBufferedSize = 0;
  mBufferOffset = 0;
  mClosed = false;
}
 
Example #15
Source File: FlexByteArrayPool.java    From fresco with MIT License 5 votes vote down vote up
public FlexByteArrayPool(MemoryTrimmableRegistry memoryTrimmableRegistry, PoolParams params) {
  Preconditions.checkArgument(params.maxNumThreads > 0);
  mDelegatePool =
      new SoftRefByteArrayPool(
          memoryTrimmableRegistry, params, NoOpPoolStatsTracker.getInstance());
  mResourceReleaser =
      new ResourceReleaser<byte[]>() {
        @Override
        public void release(byte[] unused) {
          FlexByteArrayPool.this.release(unused);
        }
      };
}
 
Example #16
Source File: PooledByteArrayBufferedInputStream.java    From fresco with MIT License 5 votes vote down vote up
public PooledByteArrayBufferedInputStream(
    InputStream inputStream, byte[] byteArray, ResourceReleaser<byte[]> resourceReleaser) {
  mInputStream = Preconditions.checkNotNull(inputStream);
  mByteArray = Preconditions.checkNotNull(byteArray);
  mResourceReleaser = Preconditions.checkNotNull(resourceReleaser);
  mBufferedSize = 0;
  mBufferOffset = 0;
  mClosed = false;
}
 
Example #17
Source File: CloseableStaticBitmap.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of a CloseableStaticBitmap.
 *
 * @param bitmap the bitmap to wrap
 * @param resourceReleaser ResourceReleaser to release the bitmap to
 */
public CloseableStaticBitmap(
    Bitmap bitmap,
    ResourceReleaser<Bitmap> resourceReleaser,
    QualityInfo qualityInfo) {
  mBitmap = Preconditions.checkNotNull(bitmap);
  mBitmapReference = CloseableReference.of(
      mBitmap,
      Preconditions.checkNotNull(resourceReleaser));
  mQualityInfo = qualityInfo;
}
 
Example #18
Source File: CloseableStaticBitmapTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  mBitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
  ResourceReleaser<Bitmap> releaser = SimpleBitmapReleaser.getInstance();
  mCloseableStaticBitmap =
      new CloseableStaticBitmap(
          mBitmap,
          releaser,
          ImmutableQualityInfo.FULL_QUALITY,
          0,
          ExifInterface.ORIENTATION_NORMAL);
}
 
Example #19
Source File: CloseableStaticBitmap.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Creates a new instance of a CloseableStaticBitmap.
 *
 * @param bitmap the bitmap to wrap
 * @param resourceReleaser ResourceReleaser to release the bitmap to
 */
public CloseableStaticBitmap(
    Bitmap bitmap,
    ResourceReleaser<Bitmap> resourceReleaser,
    QualityInfo qualityInfo,
    int rotationAngle,
    int exifOrientation) {
  mBitmap = Preconditions.checkNotNull(bitmap);
  mBitmapReference = CloseableReference.of(mBitmap, Preconditions.checkNotNull(resourceReleaser));
  mQualityInfo = qualityInfo;
  mRotationAngle = rotationAngle;
  mExifOrientation = exifOrientation;
}
 
Example #20
Source File: CloseableStaticBitmap.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Creates a new instance of a CloseableStaticBitmap.
 *
 * @param bitmap the bitmap to wrap
 * @param resourceReleaser ResourceReleaser to release the bitmap to
 */
public CloseableStaticBitmap(
    Bitmap bitmap,
    ResourceReleaser<Bitmap> resourceReleaser,
    QualityInfo qualityInfo,
    int rotationAngle) {
  this(bitmap, resourceReleaser, qualityInfo, rotationAngle, ExifInterface.ORIENTATION_UNDEFINED);
}
 
Example #21
Source File: CountingMemoryCache.java    From fresco with MIT License 5 votes vote down vote up
/** Creates a new reference for the client. */
private synchronized CloseableReference<V> newClientReference(final Entry<K, V> entry) {
  increaseClientCount(entry);
  return CloseableReference.of(
      entry.valueRef.get(),
      new ResourceReleaser<V>() {
        @Override
        public void release(V unused) {
          releaseClientReference(entry);
        }
      });
}
 
Example #22
Source File: BitmapCounter.java    From fresco with MIT License 4 votes vote down vote up
public ResourceReleaser<Bitmap> getReleaser() {
  return mUnpooledBitmapsReleaser;
}
 
Example #23
Source File: AnimatedDrawableCachingBackendImpl.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
public AnimatedDrawableCachingBackendImpl(
    SerialExecutorService executorService,
    ActivityManager activityManager,
    AnimatedDrawableUtil animatedDrawableUtil,
    MonotonicClock monotonicClock,
    AnimatedDrawableBackend animatedDrawableBackend,
    AnimatedDrawableOptions options) {
  super(animatedDrawableBackend);
  mExecutorService = executorService;
  mActivityManager = activityManager;
  mAnimatedDrawableUtil = animatedDrawableUtil;
  mMonotonicClock = monotonicClock;
  mAnimatedDrawableBackend = animatedDrawableBackend;
  mAnimatedDrawableOptions = options;
  mMaximumBytes = options.maximumBytes >= 0 ?
      options.maximumBytes : getDefaultMaxBytes(activityManager);
  mAnimatedImageCompositor = new AnimatedImageCompositor(
      animatedDrawableBackend,
      new AnimatedImageCompositor.Callback() {
        @Override
        public void onIntermediateResult(int frameNumber, Bitmap bitmap) {
          maybeCacheBitmapDuringRender(frameNumber, bitmap);
        }

        @Override
        public CloseableReference<Bitmap> getCachedBitmap(int frameNumber) {
          synchronized (AnimatedDrawableCachingBackendImpl.this) {
            return CloseableReference.cloneOrNull(mCachedBitmaps.get(frameNumber));
          }
        }
      });
  mResourceReleaserForBitmaps = new ResourceReleaser<Bitmap>() {
    @Override
    public void release(Bitmap value) {
      releaseBitmapInternal(value);
    }
  };
  mFreeBitmaps = new ArrayList<Bitmap>();
  mDecodesInFlight = new SparseArrayCompat<Task<Object>>(10);
  mCachedBitmaps = new SparseArrayCompat<CloseableReference<Bitmap>>(10);
  mBitmapsToKeepCached = new WhatToKeepCachedArray(mAnimatedDrawableBackend.getFrameCount());
  mApproxBytesToHoldAllFrames =
      mAnimatedDrawableBackend.getFrameCount() *
          mAnimatedDrawableBackend.getRenderedWidth() *
          mAnimatedDrawableBackend.getRenderedHeight() * 4;
}
 
Example #24
Source File: CloseableReferenceFactory.java    From fresco with MIT License 4 votes vote down vote up
public <T> CloseableReference<T> create(T t, ResourceReleaser<T> resourceReleaser) {
  return CloseableReference.of(t, resourceReleaser, mLeakHandler);
}