Esempio n. 1
0
 /** Gets the {@code DrawableParent} for index. */
 public DrawableParent getDrawableParentForIndex(int index) {
   Preconditions.checkArgument(index >= 0);
   Preconditions.checkArgument(index < mDrawableParents.length);
   if (mDrawableParents[index] == null) {
     mDrawableParents[index] = createDrawableParentForIndex(index);
   }
   return mDrawableParents[index];
 }
 public PostprocessorProducer(
     Producer<CloseableReference<CloseableImage>> inputProducer,
     PlatformBitmapFactory platformBitmapFactory,
     Executor executor) {
   mInputProducer = Preconditions.checkNotNull(inputProducer);
   mBitmapFactory = platformBitmapFactory;
   mExecutor = Preconditions.checkNotNull(executor);
 }
 public ThrottlingProducer(
     int maxSimultaneousRequests, Executor executor, final Producer<T> inputProducer) {
   mMaxSimultaneousRequests = maxSimultaneousRequests;
   mExecutor = Preconditions.checkNotNull(executor);
   mInputProducer = Preconditions.checkNotNull(inputProducer);
   mPendingRequests = new ConcurrentLinkedQueue<Pair<Consumer<T>, ProducerContext>>();
   mNumCurrentRequests = 0;
 }
Esempio n. 4
0
 public NetworkImagesProgressiveDecoder(
     final Consumer<CloseableReference<CloseableImage>> consumer,
     final ProducerContext producerContext,
     final ProgressiveJpegParser progressiveJpegParser,
     final ProgressiveJpegConfig progressiveJpegConfig) {
   super(consumer, producerContext);
   mProgressiveJpegParser = Preconditions.checkNotNull(progressiveJpegParser);
   mProgressiveJpegConfig = Preconditions.checkNotNull(progressiveJpegConfig);
   mLastScheduledScanNumber = 0;
 }
 /** Validates the parameters before building a controller. */
 protected void validate() {
   Preconditions.checkState(
       (mMultiImageRequests == null) || (mImageRequest == null),
       "Cannot specify both ImageRequest and FirstAvailableImageRequests!");
   Preconditions.checkState(
       (mDataSourceSupplier == null)
           || (mMultiImageRequests == null
               && mImageRequest == null
               && mLowResImageRequest == null),
       "Cannot specify DataSourceSupplier with other ImageRequests! Use one or the other.");
 }
 public DecodeProducer(
     final ByteArrayPool byteArrayPool,
     final Executor executor,
     final ImageDecoder imageDecoder,
     final ProgressiveJpegConfig progressiveJpegConfig,
     final Producer<CloseableReference<PooledByteBuffer>> nextProducer) {
   mByteArrayPool = Preconditions.checkNotNull(byteArrayPool);
   mExecutor = Preconditions.checkNotNull(executor);
   mImageDecoder = Preconditions.checkNotNull(imageDecoder);
   mProgressiveJpegConfig = Preconditions.checkNotNull(progressiveJpegConfig);
   mNextProducer = Preconditions.checkNotNull(nextProducer);
 }
 /**
  * Creates a new inputstream instance over the specific memory chunk.
  *
  * @param nativeMemoryChunk the native memory chunk
  * @param startOffset start offset within the memory chunk
  * @param length length of subchunk
  */
 public NativeMemoryChunkInputStream(
     NativeMemoryChunk nativeMemoryChunk, int startOffset, int length) {
   super();
   Preconditions.checkState(startOffset >= 0);
   Preconditions.checkState(length >= 0);
   mMemoryChunk = Preconditions.checkNotNull(nativeMemoryChunk);
   mStartOffset = startOffset;
   mEndOffset =
       startOffset + length > nativeMemoryChunk.getSize()
           ? nativeMemoryChunk.getSize()
           : startOffset + length;
   mOffset = startOffset;
   mMark = startOffset;
 }
Esempio n. 8
0
 /** Wraps the parent's child with a ScaleTypeDrawable. */
 static ScaleTypeDrawable wrapChildWithScaleType(DrawableParent parent, ScaleType scaleType) {
   Drawable child = parent.setDrawable(sEmptyDrawable);
   child = maybeWrapWithScaleType(child, scaleType);
   parent.setDrawable(child);
   Preconditions.checkNotNull(child, "Parent has no child drawable!");
   return (ScaleTypeDrawable) child;
 }
Esempio n. 9
0
  private CloseableReference<Bitmap> decodeStaticImageFromStream(
      InputStream inputStream, BitmapFactory.Options options) {
    Preconditions.checkNotNull(inputStream);
    int sizeInBytes =
        BitmapUtil.getSizeInByteForBitmap(
            options.outWidth, options.outHeight, options.inPreferredConfig);
    final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes);
    if (bitmapToReuse == null) {
      throw new NullPointerException("BitmapPool.get returned null");
    }
    options.inBitmap = bitmapToReuse;

    Bitmap decodedBitmap;
    ByteBuffer byteBuffer = mDecodeBuffers.acquire();
    if (byteBuffer == null) {
      byteBuffer = ByteBuffer.allocate(DECODE_BUFFER_SIZE);
    }
    try {
      options.inTempStorage = byteBuffer.array();
      decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
    } catch (RuntimeException re) {
      mBitmapPool.release(bitmapToReuse);
      throw re;
    } finally {
      mDecodeBuffers.release(byteBuffer);
    }

    if (bitmapToReuse != decodedBitmap) {
      mBitmapPool.release(bitmapToReuse);
      decodedBitmap.recycle();
      throw new IllegalStateException();
    }

    return CloseableReference.of(decodedBitmap, mBitmapPool);
  }
 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);
   }
 }
Esempio n. 11
0
  /**
   * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
   *
   * @param encodedImage the encoded image with reference to the encoded bytes
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
   *     Bitmap
   * @param length the number of encoded bytes in the buffer
   * @return the bitmap
   * @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
  @Override
  public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(
      EncodedImage encodedImage, Bitmap.Config bitmapConfig, int length) {
    boolean isJpegComplete = encodedImage.isCompleteAt(length);
    final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);

    InputStream jpegDataStream = encodedImage.getInputStream();
    // At this point the InputStream from the encoded image should not be null since in the
    // pipeline,this comes from a call stack where this was checked before. Also this method needs
    // the InputStream to decode the image so this can't be null.
    Preconditions.checkNotNull(jpegDataStream);
    if (encodedImage.getSize() > length) {
      jpegDataStream = new LimitedInputStream(jpegDataStream, length);
    }
    if (!isJpegComplete) {
      jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
    }
    boolean retryOnFail = options.inPreferredConfig != Bitmap.Config.ARGB_8888;
    try {
      return decodeStaticImageFromStream(jpegDataStream, options);
    } catch (RuntimeException re) {
      if (retryOnFail) {
        return decodeFromEncodedImage(encodedImage, Bitmap.Config.ARGB_8888);
      }
      throw re;
    }
  }
Esempio n. 12
0
 public DecodeProducer(
     final ByteArrayPool byteArrayPool,
     final Executor executor,
     final ImageDecoder imageDecoder,
     final ProgressiveJpegConfig progressiveJpegConfig,
     final boolean downsampleEnabled,
     final boolean downsampleEnabledForNetwork,
     final Producer<EncodedImage> inputProducer) {
   mByteArrayPool = Preconditions.checkNotNull(byteArrayPool);
   mExecutor = Preconditions.checkNotNull(executor);
   mImageDecoder = Preconditions.checkNotNull(imageDecoder);
   mProgressiveJpegConfig = Preconditions.checkNotNull(progressiveJpegConfig);
   mDownsampleEnabled = downsampleEnabled;
   mDownsampleEnabledForNetwork = downsampleEnabledForNetwork;
   mInputProducer = Preconditions.checkNotNull(inputProducer);
 }
Esempio n. 13
0
 /**
  * Constructs a new layer drawable.
  *
  * @param layers the layers that this drawable displays
  */
 public ArrayDrawable(Drawable[] layers) {
   Preconditions.checkNotNull(layers);
   mLayers = layers;
   for (int i = 0; i < mLayers.length; i++) {
     DrawableUtils.setCallbacks(mLayers[i], this, this);
   }
   mDrawableParents = new DrawableParent[mLayers.length];
 }
Esempio n. 14
0
  /**
   * Creates a {@link WebPImage} from the specified encoded data. This will throw if it fails to
   * create. This is meant to be called on a worker thread.
   *
   * @param source the data to the image (a copy will be made)
   */
  public static WebPImage create(byte[] source) {
    ensure();
    Preconditions.checkNotNull(source);

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(source.length);
    byteBuffer.put(source);
    byteBuffer.rewind();

    return nativeCreateFromDirectByteBuffer(byteBuffer);
  }
Esempio n. 15
0
  /** Sets a new drawable at the specified index, and return the previous drawable, if any. */
  @Nullable
  public Drawable setDrawable(int index, @Nullable Drawable drawable) {
    Preconditions.checkArgument(index >= 0);
    Preconditions.checkArgument(index < mLayers.length);
    final Drawable oldDrawable = mLayers[index];
    if (drawable != oldDrawable) {
      if (drawable != null && mIsMutated) {
        drawable.mutate();
      }

      DrawableUtils.setCallbacks(mLayers[index], null, null);
      DrawableUtils.setCallbacks(drawable, null, null);
      DrawableUtils.setDrawableProperties(drawable, mDrawableProperties);
      DrawableUtils.copyProperties(drawable, this);
      DrawableUtils.setCallbacks(drawable, this, this);
      mIsStatefulCalculated = false;
      mLayers[index] = drawable;
      invalidateSelf();
    }
    return oldDrawable;
  }
Esempio n. 16
0
 public SerialDelegatingExecutor(Executor delegate) {
   mDelegate = Preconditions.checkNotNull(delegate);
   mExecutionInProgress = false;
   mCommands = new LinkedList<Runnable>();
   mRunnable =
       new Runnable() {
         @Override
         public void run() {
           executeSingleCommand();
         }
       };
 }
 public BitmapMemoryCacheKey(
     String sourceString,
     @Nullable ResizeOptions resizeOptions,
     boolean autoRotated,
     ImageDecodeOptions imageDecodeOptions) {
   mSourceString = Preconditions.checkNotNull(sourceString);
   mResizeOptions = resizeOptions;
   mAutoRotated = autoRotated;
   mImageDecodeOptions = imageDecodeOptions;
   mHash =
       HashCodeUtil.hashCode(
           sourceString.hashCode(),
           (resizeOptions != null) ? resizeOptions.hashCode() : 0,
           autoRotated ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(),
           mImageDecodeOptions);
 }
Esempio n. 18
0
 /**
  * Submits a request for execution and returns a DataSource representing the pending encoded
  * image(s).
  *
  * <p>The ResizeOptions in the imageRequest will be ignored for this fetch
  *
  * <p>The returned DataSource must be closed once the client has finished with it.
  *
  * @param imageRequest the request to submit
  * @return a DataSource representing the pending encoded image(s)
  */
 public DataSource<CloseableReference<PooledByteBuffer>> fetchEncodedImage(
     ImageRequest imageRequest, Object callerContext) {
   Preconditions.checkNotNull(imageRequest.getSourceUri());
   try {
     Producer<CloseableReference<PooledByteBuffer>> producerSequence =
         mProducerSequenceFactory.getEncodedImageProducerSequence(imageRequest);
     // The resize options are used to determine whether images are going to be downsampled during
     // decode or not. For the case where the image has to be downsampled and it's a local image it
     // will be kept as a FileInputStream until decoding instead of reading it in memory. Since
     // this method returns an encoded image, it should always be read into memory. Therefore, the
     // resize options are ignored to avoid treating the image as if it was to be downsampled
     // during decode.
     if (imageRequest.getResizeOptions() != null) {
       imageRequest = ImageRequestBuilder.fromRequest(imageRequest).setResizeOptions(null).build();
     }
     return submitFetchRequest(
         producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext);
   } catch (Exception exception) {
     return DataSources.immediateFailedDataSource(exception);
   }
 }
Esempio n. 19
0
 /**
  * Gets the drawable at the specified index.
  *
  * @param index index of drawable to get
  * @return drawable at the specified index
  */
 @Nullable
 public Drawable getDrawable(int index) {
   Preconditions.checkArgument(index >= 0);
   Preconditions.checkArgument(index < mLayers.length);
   return mLayers[index];
 }
Esempio n. 20
0
 public SimpleCacheKey(String s) {
   mKey = (String) Preconditions.checkNotNull(s);
 }
Esempio n. 21
0
 public static WebPImage create(long nativePtr, int sizeInBytes) {
   ensure();
   Preconditions.checkArgument(nativePtr != 0);
   return nativeCreateFromNativeMemory(nativePtr, sizeInBytes);
 }
 public ImageUrlsRequestBuilder(final String endpointUrl) {
   mEndpointUrl = Preconditions.checkNotNull(endpointUrl);
   mRequestedImageFormats = new HashMap<>();
 }