Exemple #1
0
 @Override
 protected void onDestroy() {
   super.onDestroy();
   if (mCurrentBitmapReference != null && mCurrentBitmapReference.isValid()) {
     mCurrentBitmapReference.close();
   }
 }
 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);
   }
 }
 private synchronized boolean setRunningIfDirtyAndNotRunning() {
   if (!mIsClosed
       && mIsDirty
       && !mIsPostProcessingRunning
       && CloseableReference.isValid(mSourceImageRef)) {
     mIsPostProcessingRunning = true;
     return true;
   }
   return false;
 }
 /** 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;
 }
 @Override
 protected void onNewResultImpl(CloseableReference<CloseableImage> newResult, boolean isLast) {
   if (!CloseableReference.isValid(newResult)) {
     // try to propagate if the last result is invalid
     if (isLast) {
       maybeNotifyOnNewResult(null, true);
     }
     // ignore if invalid
     return;
   }
   updateSourceImageRef(newResult, isLast);
 }
Exemple #6
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);
   }
 }
 @Test
 public void testBitmapMemoryCacheGetSuccessful() {
   setupBitmapCacheGetSuccess();
   when(mProducerContext.getLowestPermittedRequestLevel())
       .thenReturn(ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE);
   mBitmapMemoryCacheGetProducer.produceResults(mConsumer, mProducerContext);
   verify(mConsumer).onNewResult(mFinalImageReference, true);
   verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
   Map<String, String> extraMap =
       ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "true");
   verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
   Assert.assertTrue(!mFinalImageReference.isValid());
 }
Exemple #8
0
  public void setImageBitmap(CloseableReference<Bitmap> bitmapReference) {
    // Note: This is not a recommended way to load Bitmap.
    // This sample is intended to test the internal methods, and show how to use them.
    Bitmap bitmap = bitmapReference.get();
    mImageView.setImageBitmap(bitmap);
    mImageView.getLayoutParams().width = (int) (bitmap.getWidth() * mConversionFactor);
    mImageView.getLayoutParams().height = (int) (bitmap.getHeight() * mConversionFactor);

    if (mCurrentBitmapReference != null && mCurrentBitmapReference.isValid()) {
      mCurrentBitmapReference.close();
    }
    mCurrentBitmapReference = bitmapReference;
  }
 @Test
 public void testEncodedMemoryCacheGetSuccessful() {
   setupEncodedMemoryCacheGetSuccess();
   when(mProducerContext.getLowestPermittedRequestLevel())
       .thenReturn(ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE);
   mEncodedMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
   ArgumentCaptor<EncodedImage> argumentCaptor = ArgumentCaptor.forClass(EncodedImage.class);
   verify(mConsumer).onNewResult(argumentCaptor.capture(), eq(true));
   EncodedImage encodedImage = argumentCaptor.getValue();
   Assert.assertSame(
       mFinalEncodedImage.getUnderlyingReferenceTestOnly(),
       encodedImage.getUnderlyingReferenceTestOnly());
   verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
   Map<String, String> extraMap = ImmutableMap.of(EncodedMemoryCacheProducer.VALUE_FOUND, "true");
   verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
   Assert.assertFalse(mFinalImageReference.isValid());
 }
 @Override
 protected synchronized boolean updateDecodeJob(
     CloseableReference<PooledByteBuffer> imageBytesRef, boolean isLast) {
   boolean ret = super.updateDecodeJob(imageBytesRef, isLast);
   if (!isLast && CloseableReference.isValid(imageBytesRef)) {
     if (!mProgressiveJpegParser.parseMoreData(imageBytesRef)) {
       return false;
     }
     int scanNum = mProgressiveJpegParser.getBestScanNumber();
     if (scanNum <= mLastScheduledScanNumber
         || scanNum
             < mProgressiveJpegConfig.getNextScanNumberToDecode(mLastScheduledScanNumber)) {
       return false;
     }
     mLastScheduledScanNumber = scanNum;
   }
   return ret;
 }
    /** 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);
      }
    }