Java Code Examples for com.facebook.common.references.CloseableReference#cloneOrNull()

The following examples show how to use com.facebook.common.references.CloseableReference#cloneOrNull() . 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: PostprocessorProducer.java    From fresco with MIT License 6 votes vote down vote up
private void updateSourceImageRef(
    @Nullable CloseableReference<CloseableImage> sourceImageRef, int status) {
  CloseableReference<CloseableImage> oldSourceImageRef;
  boolean shouldSubmit;
  synchronized (PostprocessorConsumer.this) {
    if (mIsClosed) {
      return;
    }
    oldSourceImageRef = mSourceImageRef;
    mSourceImageRef = CloseableReference.cloneOrNull(sourceImageRef);
    mStatus = status;
    mIsDirty = true;
    shouldSubmit = setRunningIfDirtyAndNotRunning();
  }
  CloseableReference.closeSafely(oldSourceImageRef);
  if (shouldSubmit) {
    submitPostprocessing();
  }
}
 
Example 2
Source File: ReScalePostprocessor.java    From WindowImageView with MIT License 6 votes vote down vote up
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
    float scale = 1.0f * width / sourceBitmap.getWidth();
    Log.e("ReScalePostprocessor", "scale:" + scale);

    scaledWidth = (int) (sourceBitmap.getWidth() * scale);
    scaledHeight = (int) (sourceBitmap.getHeight() * scale);

    listener.onProcessFinished(scaledWidth, scaledHeight);

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Bitmap bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
    CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(bitmap);
    try {
        return CloseableReference.cloneOrNull(bitmapRef);
    } finally {
        CloseableReference.closeSafely(bitmapRef);
    }
}
 
Example 3
Source File: PorterDuffXfermodePostProcessor.java    From react-native-image-filter-kit with MIT License 5 votes vote down vote up
@Override
protected CloseableReference<Bitmap> processComposition(
  Bitmap dstImage,
  Bitmap srcImage,
  PlatformBitmapFactory bitmapFactory
) {
  final CloseableReference<Bitmap> outRef = bitmapFactory.createBitmap(
    canvasExtent(dstImage.getWidth(), srcImage.getWidth(), mWidth),
    canvasExtent(dstImage.getHeight(), srcImage.getHeight(), mHeight)
  );

  try {
    final Canvas canvas = new Canvas(outRef.get());
    final int flags = Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG;
    final Paint paint = new Paint(flags);

    if (mSwapImages) {
      drawSrc(canvas, srcImage, paint);

    } else {
      drawDst(canvas, dstImage, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(mMode));

    if (mSwapImages) {
      drawDst(canvas, dstImage, paint);

    } else {
      drawSrc(canvas, srcImage, paint);
    }

    return CloseableReference.cloneOrNull(outRef);
  } finally {
    CloseableReference.closeSafely(outRef);
  }
}
 
Example 4
Source File: DokitFrescoPostprocessor.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
    try {
        if (PerformanceSpInfoConfig.isLargeImgOpen()) {
            double imgSize = ConvertUtils.byte2MemorySize(sourceBitmap.getByteCount(), MemoryConstants.MB);
            LargePictureManager.getInstance().saveImageInfo(mUri.toString(), imgSize, sourceBitmap.getWidth(), sourceBitmap.getHeight(), "Fresco");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (mOriginalPostprocessor != null) {
        return mOriginalPostprocessor.process(sourceBitmap, bitmapFactory);
    }

    final Bitmap.Config sourceBitmapConfig = sourceBitmap.getConfig();
    CloseableReference<Bitmap> destBitmapRef =
            bitmapFactory.createBitmapInternal(
                    sourceBitmap.getWidth(),
                    sourceBitmap.getHeight(),
                    sourceBitmapConfig != null ? sourceBitmapConfig : FALLBACK_BITMAP_CONFIGURATION);
    try {
        process(destBitmapRef.get(), sourceBitmap);
        return CloseableReference.cloneOrNull(destBitmapRef);
    } finally {
        CloseableReference.closeSafely(destBitmapRef);
    }

}
 
Example 5
Source File: PostprocessorProducer.java    From fresco with MIT License 5 votes vote down vote up
private void setSourceImageRef(CloseableReference<CloseableImage> sourceImageRef) {
  CloseableReference<CloseableImage> oldSourceImageRef;
  synchronized (RepeatedPostprocessorConsumer.this) {
    if (mIsClosed) {
      return;
    }
    oldSourceImageRef = mSourceImageRef;
    mSourceImageRef = CloseableReference.cloneOrNull(sourceImageRef);
  }
  CloseableReference.closeSafely(oldSourceImageRef);
}
 
Example 6
Source File: BaseFrescoDrawable.java    From fresco with MIT License 5 votes vote down vote up
public @Nullable Drawable setImage(
    @Nullable Drawable imageDrawable,
    @Nullable CloseableReference<CloseableImage> imageReference) {
  CloseableReference.closeSafely(mImageReference);
  mImageReference = CloseableReference.cloneOrNull(imageReference);
  return setDrawable(IMAGE_DRAWABLE_INDEX, imageDrawable);
}
 
Example 7
Source File: KeepLastFrameCache.java    From fresco with MIT License 5 votes vote down vote up
@Nullable
@Override
public synchronized CloseableReference<Bitmap> getCachedFrame(int frameNumber) {
  if (mLastFrameNumber == frameNumber) {
    return CloseableReference.cloneOrNull(mLastBitmapReference);
  }
  return null;
}
 
Example 8
Source File: RenderscriptCompositionPostProcessor.java    From react-native-image-filter-kit with MIT License 4 votes vote down vote up
@Override
protected CloseableReference<Bitmap> processComposition(
  Bitmap dstImage,
  Bitmap srcImage,
  PlatformBitmapFactory bitmapFactory
) {
  final int width = canvasExtent(dstImage.getWidth(), srcImage.getWidth(), mWidth);
  final int height = canvasExtent(dstImage.getHeight(), srcImage.getHeight(), mHeight);

  final CloseableReference<Bitmap> tmpDstRef = bitmapFactory.createBitmap(width, height);
  final CloseableReference<Bitmap> tmpSrcRef = bitmapFactory.createBitmap(width, height);
  final CloseableReference<Bitmap> outRef = bitmapFactory.createBitmap(width, height);

  try {
    final Bitmap out = outRef.get();
    final Bitmap tmpDst = tmpDstRef.get();
    final Bitmap tmpSrc = tmpSrcRef.get();

    final Canvas dstCanvas = new Canvas(tmpDst);
    final Canvas srcCanvas = new Canvas(tmpSrc);
    final int flags = Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG;
    final Paint paint = new Paint(flags);

    dstCanvas.drawBitmap(
      dstImage,
      bitmapTransform(width, height, dstImage.getWidth(), dstImage.getHeight(), mDstTransform),
      paint
    );

    srcCanvas.drawBitmap(
      srcImage,
      bitmapTransform(width, height, srcImage.getWidth(), srcImage.getHeight(), mSrcTransform),
      paint
    );

    processRenderscriptComposition(
      mSwapImages ? tmpSrc : tmpDst,
      mSwapImages ? tmpDst : tmpSrc,
      out
    );

    return CloseableReference.cloneOrNull(outRef);
  } finally {
    CloseableReference.closeSafely(tmpDstRef);
    CloseableReference.closeSafely(tmpSrcRef);
    CloseableReference.closeSafely(outRef);
  }
}
 
Example 9
Source File: CloseableProducerToDataSourceAdapter.java    From fresco with MIT License 4 votes vote down vote up
@Override
protected void onNewResultImpl(
    CloseableReference<T> result, int status, ProducerContext producerContext) {
  super.onNewResultImpl(CloseableReference.cloneOrNull(result), status, producerContext);
}
 
Example 10
Source File: KeepLastFrameCache.java    From fresco with MIT License 4 votes vote down vote up
@Nullable
@Override
public synchronized CloseableReference<Bitmap> getFallbackFrame(int frameNumber) {
  return CloseableReference.cloneOrNull(mLastBitmapReference);
}
 
Example 11
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 12
Source File: FrescoStateImpl.java    From fresco with MIT License 4 votes vote down vote up
@Override
@Nullable
public synchronized CloseableReference<CloseableImage> getCachedImage() {
  return CloseableReference.cloneOrNull(mCachedImage);
}
 
Example 13
Source File: FrescoStateImpl.java    From fresco with MIT License 4 votes vote down vote up
@Override
public synchronized void setCachedImage(
    @Nullable CloseableReference<CloseableImage> cachedImage) {
  CloseableReference.closeSafely(mCachedImage);
  mCachedImage = CloseableReference.cloneOrNull(cachedImage);
}
 
Example 14
Source File: CloseableProducerToDataSourceAdapter.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onNewResultImpl(CloseableReference<T> result, boolean isLast) {
  super.onNewResultImpl(CloseableReference.cloneOrNull(result), isLast);
}
 
Example 15
Source File: BitmapMemoryCacheKeyMultiplexProducer.java    From fresco with MIT License 4 votes vote down vote up
public CloseableReference<CloseableImage> cloneOrNull(
    CloseableReference<CloseableImage> closeableImage) {
  return CloseableReference.cloneOrNull(closeableImage);
}
 
Example 16
Source File: AnimatedImageResult.java    From fresco with MIT License 3 votes vote down vote up
/**
 * Gets a decoded frame. This will only return non-null if the {@code ImageDecodeOptions} were
 * configured to decode all frames at decode time.
 *
 * @param index the index of the frame to get
 * @return a reference to the preview bitmap which must be released by the caller when done or
 *     null if there is no preview bitmap set
 */
public synchronized @Nullable CloseableReference<Bitmap> getDecodedFrame(int index) {
  if (mDecodedFrames != null) {
    return CloseableReference.cloneOrNull(mDecodedFrames.get(index));
  }
  return null;
}
 
Example 17
Source File: EncodedImage.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Returns a cloned reference to the stored encoded bytes.
 *
 * <p>The caller has to close the reference once it has finished using it.
 */
public CloseableReference<PooledByteBuffer> getByteBufferRef() {
  return CloseableReference.cloneOrNull(mPooledByteBufferRef);
}
 
Example 18
Source File: CloseableStaticBitmap.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Get a cloned bitmap reference for the underlying original CloseableReference&lt;Bitmap&gt;.
 *
 * <p>After calling this method, this object can still be used. See {@link
 * #convertToBitmapReference()} for an alternative that detaches the original reference instead.
 *
 * @return the cloned bitmap reference without altering this instance or null if already closed
 */
@Nullable
public synchronized CloseableReference<Bitmap> cloneUnderlyingBitmapReference() {
  return CloseableReference.cloneOrNull(mBitmapReference);
}
 
Example 19
Source File: AnimatedImageResultBuilder.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Gets the decoded frames. Only used if the {@code ImageDecodeOptions} were configured to decode
 * all frames at decode time.
 *
 * @return the references to the decoded frames or null if none was set. This returns references
 *     that must be released by the caller
 */
public List<CloseableReference<Bitmap>> getDecodedFrames() {
  return CloseableReference.cloneOrNull(mDecodedFrames);
}
 
Example 20
Source File: SettableDataSource.java    From fresco with MIT License 2 votes vote down vote up
/**
 * Sets the value of this data source.
 *
 * <p>This method will return {@code true} if the value was successfully set, or {@code false} if
 * the data source has already been set, failed or closed.
 *
 * <p>Passed CloseableReference is cloned, caller of this method still owns passed reference after
 * the method returns.
 *
 * @param valueRef closeable reference to the value the data source should hold.
 * @return true if the value was successfully set.
 */
public boolean set(@Nullable CloseableReference<T> valueRef) {
  CloseableReference<T> clonedRef = CloseableReference.cloneOrNull(valueRef);
  return super.setResult(clonedRef, /* isLast */ true, null);
}