Ejemplo n.º 1
0
        /// <summary>
        /// Returns a new CloseableReference to the same underlying
        /// SharedReference or null if invalid. The SharedReference
        /// ref-count is incremented.
        /// </summary>
        public EncodedImage CloneOrNull()
        {
            EncodedImage encodedImage;

            if (_inputStreamSupplier != null)
            {
                encodedImage = new EncodedImage(_inputStreamSupplier, StreamSize);
            }
            else
            {
                CloseableReference <IPooledByteBuffer> pooledByteBufferRef =
                    CloseableReference <IPooledByteBuffer> .CloneOrNull(_pooledByteBufferRef);

                try
                {
                    encodedImage = (pooledByteBufferRef == null) ?
                                   null :
                                   new EncodedImage(pooledByteBufferRef);
                }
                finally
                {
                    // Close the recently created reference since it will be
                    // cloned again in the constructor.
                    CloseableReference <IPooledByteBuffer> .CloseSafely(pooledByteBufferRef);
                }
            }

            if (encodedImage != null)
            {
                encodedImage.CopyMetaDataFrom(this);
            }

            return(encodedImage);
        }
Ejemplo n.º 2
0
            private void UpdateSourceImageRef(
                CloseableReference <CloseableImage> sourceImageRef,
                bool isLast)
            {
                CloseableReference <CloseableImage> oldSourceImageRef;
                bool shouldSubmit;

                lock (_gate)
                {
                    if (_isClosed)
                    {
                        return;
                    }

                    oldSourceImageRef = _sourceImageRef;
                    _sourceImageRef   = CloseableReference <CloseableImage> .CloneOrNull(sourceImageRef);

                    _isLast      = isLast;
                    _isDirty     = true;
                    shouldSubmit = SetRunningIfDirtyAndNotRunning();
                }

                CloseableReference <CloseableImage> .CloseSafely(oldSourceImageRef);

                if (shouldSubmit)
                {
                    SubmitPostprocessing();
                }
            }
 private Entry(K key, CloseableReference <V> valueRef, IEntryStateObserver <K> observer)
 {
     Key         = Preconditions.CheckNotNull(key);
     ValueRef    = Preconditions.CheckNotNull(CloseableReference <V> .CloneOrNull(valueRef));
     ClientCount = 0;
     Orphan      = false;
     Observer    = observer;
 }
 /// <summary>
 /// Creates a new instance of a CloseableStaticBitmap from an existing
 /// CloseableReference. The CloseableStaticBitmap will hold a reference
 /// to the bitmap until it's closed.
 /// </summary>
 public CloseableStaticBitmap(
     CloseableReference <SoftwareBitmap> bitmapReference,
     IQualityInfo qualityInfo,
     int rotationAngle)
 {
     _bitmapReference = Preconditions.CheckNotNull(bitmapReference.CloneOrNull());
     _bitmap          = _bitmapReference.Get();
     _qualityInfo     = qualityInfo;
     _rotationAngle   = rotationAngle;
 }
Ejemplo n.º 5
0
            private void SetSourceImageRef(CloseableReference <CloseableImage> sourceImageRef)
            {
                CloseableReference <CloseableImage> oldSourceImageRef;

                lock (_gate)
                {
                    if (_isClosed)
                    {
                        return;
                    }

                    oldSourceImageRef = _sourceImageRef;
                    _sourceImageRef   = CloseableReference <CloseableImage> .CloneOrNull(sourceImageRef);
                }

                CloseableReference <CloseableImage> .CloseSafely(oldSourceImageRef);
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Clients should override this method only if the post-processed
        /// bitmap has to be of a different size than the source bitmap.
        /// If the post-processed bitmap is of the same size, clients should
        /// override one of the other two methods.
        ///
        /// <para />The source bitmap must not be modified as it may be shared
        /// by the other clients. The implementation must create a new bitmap
        /// that is safe to be modified and return a reference to it.
        /// Clients should use <code>bitmapFactory</code> to create a new bitmap.
        /// </summary>
        /// <param name="sourceBitmap">The source bitmap.</param>
        /// <param name="bitmapFactory">
        /// The factory to create a destination bitmap.
        /// </param>
        /// <param name="flexByteArrayPool">
        /// The memory pool used for post process.
        /// </param>
        /// <returns>
        /// A reference to the newly created bitmap.
        /// </returns>
        public CloseableReference <SoftwareBitmap> Process(
            SoftwareBitmap sourceBitmap,
            PlatformBitmapFactory bitmapFactory,
            FlexByteArrayPool flexByteArrayPool)
        {
            CloseableReference <SoftwareBitmap> destBitmapRef =
                bitmapFactory.CreateBitmapInternal(
                    sourceBitmap.PixelWidth,
                    sourceBitmap.PixelHeight,
                    sourceBitmap.BitmapPixelFormat);

            try
            {
                Process(destBitmapRef.Get(), sourceBitmap, flexByteArrayPool);
                return(CloseableReference <SoftwareBitmap> .CloneOrNull(destBitmapRef));
            }
            finally
            {
                CloseableReference <SoftwareBitmap> .CloseSafely(destBitmapRef);
            }
        }
Ejemplo n.º 7
0
            private void UpdateInternal()
            {
                CloseableReference <CloseableImage> sourceImageRef;

                lock (_gate)
                {
                    if (_isClosed)
                    {
                        return;
                    }

                    sourceImageRef = CloseableReference <CloseableImage> .CloneOrNull(_sourceImageRef);
                }

                try
                {
                    Consumer.OnNewResult(sourceImageRef, false /* isLast */);
                }
                finally
                {
                    CloseableReference <CloseableImage> .CloseSafely(sourceImageRef);
                }
            }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns a stream from the internal stream supplier if it's not null.
        /// Otherwise returns an stream for the internal buffer reference if
        /// valid and null otherwise.
        ///
        /// <para />The caller has to close the stream after using it.
        /// </summary>
        public Stream GetInputStream()
        {
            if (_inputStreamSupplier != null)
            {
                return(_inputStreamSupplier.Get());
            }

            CloseableReference <IPooledByteBuffer> pooledByteBufferRef =
                CloseableReference <IPooledByteBuffer> .CloneOrNull(_pooledByteBufferRef);

            if (pooledByteBufferRef != null)
            {
                try
                {
                    return(new PooledByteBufferInputStream(pooledByteBufferRef.Get()));
                }
                finally
                {
                    CloseableReference <IPooledByteBuffer> .CloseSafely(pooledByteBufferRef);
                }
            }

            return(null);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Gets the result if any, null otherwise.
 ///
 /// <para />Value will be cloned and it's the caller's responsibility
 /// to close the returned value.
 /// </summary>
 public override CloseableReference <T> GetResult()
 {
     return(CloseableReference <T> .CloneOrNull(base.GetResult()));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Sets the value of this data source.
        ///
        /// <para />This method will return <code> true</code> if the
        /// value was successfully set, or <code>false</code> if the
        /// data source has already been set, failed or closed.
        ///
        /// <para />Passed CloseableReference is cloned, caller of this
        /// method still owns passed reference after the method returns.
        /// </summary>
        /// <param name="valueRef">
        /// Closeable reference to the value the data source should hold.
        /// </param>
        /// <returns>true if the value was successfully set.</returns>
        public bool Set(CloseableReference <T> valueRef)
        {
            CloseableReference <T> clonedRef = CloseableReference <T> .CloneOrNull(valueRef);

            return(SetResult(clonedRef, /* isLast */ true));
        }
 /// <summary>
 /// OnNewResult implementation.
 /// </summary>
 protected override void OnNewResultImpl(CloseableReference <T> result, bool isLast)
 {
     base.OnNewResultImpl(CloseableReference <T> .CloneOrNull(result), isLast);
 }
 /// <summary>
 /// Clones the result.
 /// </summary>
 public override CloseableReference <CloseableImage> CloneOrNull(
     CloseableReference <CloseableImage> closeableImage)
 {
     return(CloseableReference <CloseableImage> .CloneOrNull(closeableImage));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Returns a cloned reference to the stored encoded bytes.
 ///
 /// <para />The caller has to close the reference once it has finished
 /// using it.
 /// </summary>
 public CloseableReference <IPooledByteBuffer> GetByteBufferRef()
 {
     return(CloseableReference <IPooledByteBuffer> .CloneOrNull(_pooledByteBufferRef));
 }