private void doPostprocessing(
     CloseableReference<CloseableImage> sourceImageRef, boolean isLast) {
   Preconditions.checkArgument(CloseableReference.isValid(sourceImageRef));
   if (!shouldPostprocess(sourceImageRef.get())) {
     maybeNotifyOnNewResult(sourceImageRef, isLast);
     return;
   }
   mListener.onProducerStart(mRequestId, NAME);
   CloseableReference<CloseableImage> destImageRef = null;
   try {
     try {
       destImageRef = postprocessInternal(sourceImageRef.get());
     } catch (Exception e) {
       mListener.onProducerFinishWithFailure(
           mRequestId, NAME, e, getExtraMap(mListener, mRequestId, mPostprocessor));
       maybeNotifyOnFailure(e);
       return;
     }
     mListener.onProducerFinishWithSuccess(
         mRequestId, NAME, getExtraMap(mListener, mRequestId, mPostprocessor));
     maybeNotifyOnNewResult(destImageRef, isLast);
   } finally {
     CloseableReference.closeSafely(destImageRef);
   }
 }
 /** Notifies consumer of new result and finishes if the result is final. */
 private void handleResult(final CloseableImage decodedImage, final boolean isFinal) {
   CloseableReference<CloseableImage> decodedImageRef = CloseableReference.of(decodedImage);
   try {
     maybeFinish(isFinal);
     getConsumer().onNewResult(decodedImageRef, isFinal);
   } finally {
     CloseableReference.closeSafely(decodedImageRef);
   }
 }
 /**
  * Finishes if not already finished and {@code finish} is specified.
  *
  * <p>If just finished, the intermediate image gets released.
  */
 private synchronized void maybeFinish(boolean finish) {
   if (mIsFinished) {
     return;
   }
   mIsFinished = finish;
   if (finish) {
     CloseableReference.closeSafely(mImageBytesRef);
     mImageBytesRef = null;
   }
 }
 private void setSourceImageRef(CloseableReference<CloseableImage> sourceImageRef) {
   CloseableReference<CloseableImage> oldSourceImageRef;
   synchronized (RepeatedPostprocessorConsumer.this) {
     if (mIsClosed) {
       return;
     }
     oldSourceImageRef = mSourceImageRef;
     mSourceImageRef = CloseableReference.cloneOrNull(sourceImageRef);
   }
   CloseableReference.closeSafely(oldSourceImageRef);
 }
 /** Updates the decode job. */
 protected synchronized boolean updateDecodeJob(
     CloseableReference<PooledByteBuffer> imageBytesRef, boolean isLast) {
   // ignore invalid intermediate results (should not happen ever, but being defensive)
   if (!isLast && !CloseableReference.isValid(imageBytesRef)) {
     return false;
   }
   CloseableReference.closeSafely(mImageBytesRef);
   mImageBytesRef = CloseableReference.cloneOrNull(imageBytesRef);
   mIsLast = isLast;
   return true;
 }
 private CloseableReference<CloseableImage> postprocessInternal(CloseableImage sourceImage) {
   CloseableStaticBitmap staticBitmap = (CloseableStaticBitmap) sourceImage;
   Bitmap sourceBitmap = staticBitmap.getUnderlyingBitmap();
   CloseableReference<Bitmap> bitmapRef = mPostprocessor.process(sourceBitmap, mBitmapFactory);
   int rotationAngle = staticBitmap.getRotationAngle();
   try {
     return CloseableReference.<CloseableImage>of(
         new CloseableStaticBitmap(bitmapRef, sourceImage.getQualityInfo(), rotationAngle));
   } finally {
     CloseableReference.closeSafely(bitmapRef);
   }
 }
Exemple #7
0
 /**
  * Returns whether the image is stored in the bitmap memory cache.
  *
  * @param imageRequest the imageRequest for the image to be looked up.
  * @return true if the image was found in the bitmap memory cache, false otherwise.
  */
 public boolean isInBitmapMemoryCache(final ImageRequest imageRequest) {
   if (imageRequest == null) {
     return false;
   }
   final CacheKey cacheKey = mCacheKeyFactory.getBitmapCacheKey(imageRequest, null);
   CloseableReference<CloseableImage> ref = mBitmapMemoryCache.get(cacheKey);
   try {
     return CloseableReference.isValid(ref);
   } finally {
     CloseableReference.closeSafely(ref);
   }
 }
 private boolean close() {
   CloseableReference<CloseableImage> oldSourceImageRef;
   synchronized (RepeatedPostprocessorConsumer.this) {
     if (mIsClosed) {
       return false;
     }
     oldSourceImageRef = mSourceImageRef;
     mSourceImageRef = null;
     mIsClosed = true;
   }
   CloseableReference.closeSafely(oldSourceImageRef);
   return true;
 }
 private void updateInternal() {
   CloseableReference<CloseableImage> sourceImageRef;
   synchronized (RepeatedPostprocessorConsumer.this) {
     if (mIsClosed) {
       return;
     }
     sourceImageRef = CloseableReference.cloneOrNull(mSourceImageRef);
   }
   try {
     getConsumer().onNewResult(sourceImageRef, false /* isLast */);
   } finally {
     CloseableReference.closeSafely(sourceImageRef);
   }
 }
 private void updateSourceImageRef(
     @Nullable CloseableReference<CloseableImage> sourceImageRef, boolean isLast) {
   CloseableReference<CloseableImage> oldSourceImageRef;
   boolean shouldSubmit;
   synchronized (PostprocessorConsumer.this) {
     if (mIsClosed) {
       return;
     }
     oldSourceImageRef = mSourceImageRef;
     mSourceImageRef = CloseableReference.cloneOrNull(sourceImageRef);
     mIsLast = isLast;
     mIsDirty = true;
     shouldSubmit = setRunningIfDirtyAndNotRunning();
   }
   CloseableReference.closeSafely(oldSourceImageRef);
   if (shouldSubmit) {
     submitPostprocessing();
   }
 }
    /** Performs the decode synchronously. */
    private void doDecode(long queueTime) {
      CloseableReference<PooledByteBuffer> bytesRef;
      boolean isLast;
      synchronized (ProgressiveDecoder.this) {
        bytesRef = mImageBytesRef;
        mImageBytesRef = null;
        isLast = mIsLast;
        mIsDecodeSubmitted = false;
        mLastDecodeTime = SystemClock.uptimeMillis();
      }

      try {
        if (isFinished() || !CloseableReference.isValid(bytesRef)) {
          return;
        }

        ImageFormat format = isLast ? ImageFormat.UNKNOWN : getImageFormat(bytesRef);
        int length = isLast ? bytesRef.get().size() : getIntermediateImageEndOffset(bytesRef);
        QualityInfo quality = isLast ? ImmutableQualityInfo.FULL_QUALITY : getQualityInfo(bytesRef);

        mProducerListener.onProducerStart(mProducerContext.getId(), PRODUCER_NAME);
        CloseableImage decodedImage;
        try {
          decodedImage =
              mImageDecoder.decodeImage(bytesRef, format, length, quality, mImageDecodeOptions);
        } catch (Exception e) {
          Map<String, String> extraMap = getExtraMap(queueTime, quality, isLast);
          mProducerListener.onProducerFinishWithFailure(
              mProducerContext.getId(), PRODUCER_NAME, e, extraMap);
          handleError(e);
          return;
        }
        Map<String, String> extraMap = getExtraMap(queueTime, quality, isLast);
        mProducerListener.onProducerFinishWithSuccess(
            mProducerContext.getId(), PRODUCER_NAME, extraMap);
        handleResult(decodedImage, isLast);
      } finally {
        CloseableReference.closeSafely(bytesRef);
      }
    }