コード例 #1
0
 /// <summary>
 /// Called after computing result successfully.
 /// </summary>
 protected override void OnSuccess(T result)
 {
     _producerListener.OnProducerFinishWithSuccess(
         _requestId,
         _producerName,
         _producerListener.RequiresExtraMap(_requestId) ? GetExtraMapOnSuccess(result) : null);
     _consumer.OnNewResult(result, true);
 }
コード例 #2
0
        internal static IDictionary <string, string> GetExtraMap(
            IProducerListener listener,
            string requestId,
            bool valueFound)
        {
            if (!listener.RequiresExtraMap(requestId))
            {
                return(null);
            }

            var extraMap = new Dictionary <string, string>()
            {
                { VALUE_FOUND, valueFound.ToString() }
            };

            return(new ReadOnlyDictionary <string, string>(extraMap));
        }
コード例 #3
0
            private IDictionary <string, string> GetExtraMap(
                IProducerListener listener,
                string requestId,
                IPostprocessor postprocessor)
            {
                if (!listener.RequiresExtraMap(requestId))
                {
                    return(null);
                }

                var extraMap = new Dictionary <string, string>()
                {
                    { POSTPROCESSOR, postprocessor.Name }
                };

                return(new ReadOnlyDictionary <string, string>(extraMap));
            }
コード例 #4
0
            private IDictionary <string, string> GetExtraMap(
                CloseableImage image,
                long queueTime,
                IQualityInfo quality,
                bool isFinal)
            {
                if (!_producerListener.RequiresExtraMap(_producerContext.Id))
                {
                    return(null);
                }

                string queueStr       = queueTime.ToString();
                string qualityStr     = quality.IsOfGoodEnoughQuality.ToString();
                string finalStr       = isFinal.ToString();
                string cacheChoiceStr = _producerContext.ImageRequest.CacheChoice.ToString();

                if (image.GetType() == typeof(CloseableStaticBitmap))
                {
                    SoftwareBitmap bitmap   = ((CloseableStaticBitmap)image).UnderlyingBitmap;
                    string         sizeStr  = bitmap.PixelWidth + "x" + bitmap.PixelHeight;
                    var            extraMap = new Dictionary <string, string>()
                    {
                        { BITMAP_SIZE_KEY, sizeStr },
                        { JobScheduler.QUEUE_TIME_KEY, queueStr },
                        { HAS_GOOD_QUALITY_KEY, qualityStr },
                        { IS_FINAL_KEY, finalStr },
                        { IMAGE_TYPE_KEY, cacheChoiceStr }
                    };

                    return(new ReadOnlyDictionary <string, string>(extraMap));
                }
                else
                {
                    var extraMap = new Dictionary <string, string>()
                    {
                        { JobScheduler.QUEUE_TIME_KEY, queueStr },
                        { HAS_GOOD_QUALITY_KEY, qualityStr },
                        { IS_FINAL_KEY, finalStr },
                        { IMAGE_TYPE_KEY, cacheChoiceStr }
                    };

                    return(new ReadOnlyDictionary <string, string>(extraMap));
                }
            }
コード例 #5
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <CloseableReference <CloseableImage> > consumer,
            IProducerContext producerContext)
        {
            IProducerListener listener  = producerContext.Listener;
            string            requestId = producerContext.Id;

            listener.OnProducerStart(requestId, ProducerName);
            ImageRequest imageRequest             = producerContext.ImageRequest;
            object       callerContext            = producerContext.CallerContext;
            ICacheKey    cacheKey                 = _cacheKeyFactory.GetBitmapCacheKey(imageRequest, callerContext);
            IDictionary <string, string> extraMap = default(IDictionary <string, string>);

            CloseableReference <CloseableImage> cachedReference = _memoryCache.Get(cacheKey);

            if (cachedReference != null)
            {
                bool isFinal = cachedReference.Get().QualityInfo.IsOfFullQuality;
                if (isFinal)
                {
                    extraMap = new Dictionary <string, string>()
                    {
                        { VALUE_FOUND, "true" }
                    };

                    listener.OnProducerFinishWithSuccess(
                        requestId,
                        ProducerName,
                        listener.RequiresExtraMap(requestId) ?
                        new ReadOnlyDictionary <string, string>(extraMap) :
                        null);

                    consumer.OnProgressUpdate(1f);
                }

                consumer.OnNewResult(cachedReference, isFinal);
                cachedReference.Dispose();
                if (isFinal)
                {
                    return;
                }
            }

            if (producerContext.LowestPermittedRequestLevel >= RequestLevel.BITMAP_MEMORY_CACHE)
            {
                extraMap = new Dictionary <string, string>()
                {
                    { VALUE_FOUND, "false" }
                };

                listener.OnProducerFinishWithSuccess(
                    requestId,
                    ProducerName,
                    listener.RequiresExtraMap(requestId) ?
                    new ReadOnlyDictionary <string, string>(extraMap) :
                    null);

                consumer.OnNewResult(null, true);
                return;
            }

            extraMap = new Dictionary <string, string>()
            {
                { VALUE_FOUND, "false" }
            };

            IConsumer <CloseableReference <CloseableImage> > wrappedConsumer =
                WrapConsumer(consumer, cacheKey);

            listener.OnProducerFinishWithSuccess(
                requestId,
                ProducerName,
                listener.RequiresExtraMap(requestId) ?
                new ReadOnlyDictionary <string, string>(extraMap) :
                null);

            _inputProducer.ProduceResults(wrappedConsumer, producerContext);
        }
コード例 #6
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <CloseableReference <CloseableImage> > consumer,
            IProducerContext producerContext)
        {
            IProducerListener listener      = producerContext.Listener;
            string            requestId     = producerContext.Id;
            ImageRequest      imageRequest  = producerContext.ImageRequest;
            object            callerContext = producerContext.CallerContext;

            // If there's no postprocessor or the postprocessor doesn't
            // require caching, forward results.
            IPostprocessor postprocessor = imageRequest.Postprocessor;

            if (postprocessor == null || postprocessor.PostprocessorCacheKey == null)
            {
                _inputProducer.ProduceResults(consumer, producerContext);
                return;
            }

            listener.OnProducerStart(requestId, ProducerName);
            ICacheKey cacheKey = _cacheKeyFactory.GetPostprocessedBitmapCacheKey(
                imageRequest, callerContext);

            CloseableReference <CloseableImage> cachedReference = _memoryCache.Get(cacheKey);
            var extraMap = default(Dictionary <string, string>);

            if (cachedReference != null)
            {
                extraMap = new Dictionary <string, string>()
                {
                    { VALUE_FOUND, "true" }
                };

                listener.OnProducerFinishWithSuccess(
                    requestId,
                    ProducerName,
                    listener.RequiresExtraMap(requestId) ?
                    new ReadOnlyDictionary <string, string>(extraMap) :
                    null);

                consumer.OnProgressUpdate(1.0f);
                consumer.OnNewResult(cachedReference, true);
                cachedReference.Dispose();
            }
            else
            {
                bool isRepeatedProcessor = postprocessor.GetType() == typeof(IRepeatedPostprocessor);
                IConsumer <CloseableReference <CloseableImage> > cachedConsumer =
                    new CachedPostprocessorConsumer(
                        consumer,
                        cacheKey,
                        isRepeatedProcessor,
                        _memoryCache);

                extraMap = new Dictionary <string, string>()
                {
                    { VALUE_FOUND, "false" }
                };

                listener.OnProducerFinishWithSuccess(
                    requestId,
                    ProducerName,
                    listener.RequiresExtraMap(requestId) ?
                    new ReadOnlyDictionary <string, string>(extraMap) :
                    null);

                _inputProducer.ProduceResults(cachedConsumer, producerContext);
            }
        }
コード例 #7
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <EncodedImage> consumer,
            IProducerContext producerContext)
        {
            string            requestId = producerContext.Id;
            IProducerListener listener  = producerContext.Listener;

            listener.OnProducerStart(requestId, PRODUCER_NAME);
            ImageRequest imageRequest = producerContext.ImageRequest;
            ICacheKey    cacheKey     = _cacheKeyFactory.GetEncodedCacheKey(
                imageRequest, producerContext.CallerContext);

            CloseableReference <IPooledByteBuffer> cachedReference = _memoryCache.Get(cacheKey);
            IDictionary <string, string>           extraMap        = default(IDictionary <string, string>);

            try
            {
                if (cachedReference != null)
                {
                    EncodedImage cachedEncodedImage = new EncodedImage(cachedReference);

                    try
                    {
                        extraMap = new Dictionary <string, string>()
                        {
                            { VALUE_FOUND, "true" }
                        };

                        listener.OnProducerFinishWithSuccess(
                            requestId,
                            PRODUCER_NAME,
                            listener.RequiresExtraMap(requestId) ?
                            new ReadOnlyDictionary <string, string>(extraMap) :
                            null);
                        consumer.OnProgressUpdate(1f);
                        consumer.OnNewResult(cachedEncodedImage, true);
                        return;
                    }
                    finally
                    {
                        EncodedImage.CloseSafely(cachedEncodedImage);
                    }
                }

                if (producerContext.LowestPermittedRequestLevel >= RequestLevel.ENCODED_MEMORY_CACHE)
                {
                    extraMap = new Dictionary <string, string>()
                    {
                        { VALUE_FOUND, "false" }
                    };

                    listener.OnProducerFinishWithSuccess(
                        requestId,
                        PRODUCER_NAME,
                        listener.RequiresExtraMap(requestId) ?
                        new ReadOnlyDictionary <string, string>(extraMap) :
                        null);

                    consumer.OnNewResult(null, true);
                    return;
                }

                IConsumer <EncodedImage> consumerOfInputProducer =
                    new EncodedMemoryCacheConsumer(_memoryCache, consumer, cacheKey);

                extraMap = new Dictionary <string, string>()
                {
                    { VALUE_FOUND, "false" }
                };

                listener.OnProducerFinishWithSuccess(
                    requestId,
                    PRODUCER_NAME,
                    listener.RequiresExtraMap(requestId) ?
                    new ReadOnlyDictionary <string, string>(extraMap) :
                    null);

                _inputProducer.ProduceResults(consumerOfInputProducer, producerContext);
            }
            finally
            {
                CloseableReference <IPooledByteBuffer> .CloseSafely(cachedReference);
            }
        }