/// <summary> /// Instantiates the <see cref="ProducerSequenceFactory"/>. /// </summary> public ProducerSequenceFactory( ProducerFactory producerFactory, INetworkFetcher <FetchState> networkFetcher, bool resizeAndRotateEnabledForNetwork, bool downsampleEnabled, bool webpSupportEnabled, ThreadHandoffProducerQueue threadHandoffProducerQueue, int throttlingMaxSimultaneousRequests, FlexByteArrayPool flexByteArrayPool) { _producerFactory = producerFactory; _networkFetcher = networkFetcher; _resizeAndRotateEnabledForNetwork = resizeAndRotateEnabledForNetwork; _downsampleEnabled = downsampleEnabled; _webpSupportEnabled = webpSupportEnabled; _postprocessorSequences = new Dictionary < IProducer <CloseableReference <CloseableImage> >, IProducer <CloseableReference <CloseableImage> > >(); _closeableImagePrefetchSequences = new Dictionary < IProducer <CloseableReference <CloseableImage> >, IProducer <object> >(); _threadHandoffProducerQueue = threadHandoffProducerQueue; _throttlingMaxSimultaneousRequests = throttlingMaxSimultaneousRequests; _flexByteArrayPool = flexByteArrayPool; }
/// <summary> /// Instantiates the <see cref="ImagePipelineCore"/>. /// </summary> /// <param name="producerSequenceFactory"> /// The factory that creates all producer sequences. /// </param> /// <param name="requestListeners"> /// The listeners for the image requests. /// </param> /// <param name="isPrefetchEnabledSupplier"> /// The supplier for enabling prefetch. /// </param> /// <param name="bitmapMemoryCache"> /// The memory cache for CloseableImage. /// </param> /// <param name="encodedMemoryCache"> /// The memory cache for IPooledByteBuffer. /// </param> /// <param name="mainBufferedDiskCache"> /// The default buffered disk cache. /// </param> /// <param name="smallImageBufferedDiskCache"> /// The buffered disk cache used for small images. /// </param> /// <param name="cacheKeyFactory"> /// The factory that creates cache keys for the pipeline. /// </param> /// <param name="threadHandoffProducerQueue"> /// Move further computation to different thread. /// </param> /// <param name="flexByteArrayPool"> /// The memory pool use for BitmapImage conversion. /// </param> public ImagePipelineCore( ProducerSequenceFactory producerSequenceFactory, HashSet <IRequestListener> requestListeners, ISupplier <bool> isPrefetchEnabledSupplier, IMemoryCache <ICacheKey, CloseableImage> bitmapMemoryCache, IMemoryCache <ICacheKey, IPooledByteBuffer> encodedMemoryCache, BufferedDiskCache mainBufferedDiskCache, BufferedDiskCache smallImageBufferedDiskCache, ICacheKeyFactory cacheKeyFactory, ThreadHandoffProducerQueue threadHandoffProducerQueue, FlexByteArrayPool flexByteArrayPool) { _idCounter = 0; _producerSequenceFactory = producerSequenceFactory; _requestListener = new ForwardingRequestListener(requestListeners); _isPrefetchEnabledSupplier = isPrefetchEnabledSupplier; _bitmapMemoryCache = bitmapMemoryCache; _encodedMemoryCache = encodedMemoryCache; _mainBufferedDiskCache = mainBufferedDiskCache; _smallImageBufferedDiskCache = smallImageBufferedDiskCache; _cacheKeyFactory = cacheKeyFactory; _threadHandoffProducerQueue = threadHandoffProducerQueue; _flexByteArrayPool = flexByteArrayPool; _handleResultExecutor = Executors.NewFixedThreadPool(MAX_DATA_SOURCE_SUBSCRIBERS); }
/// <summary> /// Instantiates the <see cref="PostprocessorProducer"/>. /// </summary> public PostprocessorProducer( IProducer <CloseableReference <CloseableImage> > inputProducer, PlatformBitmapFactory platformBitmapFactory, FlexByteArrayPool flexByteArrayPool, IExecutorService executor) { _inputProducer = Preconditions.CheckNotNull(inputProducer); _bitmapFactory = platformBitmapFactory; _flexByteArrayPool = flexByteArrayPool; _executor = Preconditions.CheckNotNull(executor); }
/// <summary> /// Clients should override this method if the post-processing cannot be /// done in place. If the post-processing can be done in place, clients /// should override the /// Process(byte[], int, int, BitmapPixelFormat, BitmapAlphaMode) method. /// /// <para />The provided destination bitmap is of the same size as the /// source bitmap. There are no guarantees on the initial content of the /// destination bitmap, so the implementation has to make sure that it /// properly populates it. /// /// <para />The source bitmap must not be modified as it may be shared /// by the other clients. The implementation must use the provided /// destination bitmap as its output. /// </summary> /// <param name="destBitmap"> /// The destination bitmap to be used as output. /// </param> /// <param name="sourceBitmap"> /// The source bitmap to be used as input. /// </param> /// <param name="flexByteArrayPool"> /// The memory pool used for post process. /// </param> public unsafe virtual void Process( SoftwareBitmap destBitmap, SoftwareBitmap sourceBitmap, FlexByteArrayPool flexByteArrayPool) { Preconditions.CheckArgument(sourceBitmap.BitmapPixelFormat == destBitmap.BitmapPixelFormat); Preconditions.CheckArgument(!destBitmap.IsReadOnly); Preconditions.CheckArgument(destBitmap.PixelWidth == sourceBitmap.PixelWidth); Preconditions.CheckArgument(destBitmap.PixelHeight == sourceBitmap.PixelHeight); sourceBitmap.CopyTo(destBitmap); using (var buffer = destBitmap.LockBuffer(BitmapBufferAccessMode.Write)) using (var reference = buffer.CreateReference()) { // Get input data byte *srcData; uint capacity; ((IMemoryBufferByteAccess)reference).GetBuffer(out srcData, out capacity); // Allocate temp buffer for processing byte[] desData = default(byte[]); CloseableReference <byte[]> bytesArrayRef = default(CloseableReference <byte[]>); try { bytesArrayRef = flexByteArrayPool.Get((int)capacity); desData = bytesArrayRef.Get(); } catch (Exception) { // Allocates the byte array since the pool couldn't provide one desData = new byte[capacity]; } try { // Process output data Marshal.Copy((IntPtr)srcData, desData, 0, (int)capacity); Process(desData, destBitmap.PixelWidth, destBitmap.PixelHeight, destBitmap.BitmapPixelFormat, destBitmap.BitmapAlphaMode); Marshal.Copy(desData, 0, (IntPtr)srcData, (int)capacity); } finally { CloseableReference <byte[]> .CloseSafely(bytesArrayRef); } } }
public void Initialize() { Dictionary <int, int> buckets = new Dictionary <int, int>(); for (int i = MIN_BUFFER_SIZE; i <= MAX_BUFFER_SIZE; i *= 2) { buckets.Add(i, 3); } _pool = new FlexByteArrayPool( new MockMemoryTrimmableRegistry(), new PoolParams( int.MaxValue, int.MaxValue, buckets, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE, 1)); _delegatePool = _pool._delegatePool; }
/// <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); } }
/// <summary> /// Instantiates the <see cref="ProducerFactory"/> /// </summary> /// <param name="byteArrayPool"> /// The IByteArrayPool used by DecodeProducer. /// </param> /// <param name="imageDecoder"> /// The image decoder. /// </param> /// <param name="progressiveJpegConfig"> /// The progressive Jpeg configuration. /// </param> /// <param name="downsampleEnabled"> /// Enabling downsample. /// </param> /// <param name="resizeAndRotateEnabledForNetwork"> /// Enabling resize and rotate. /// </param> /// <param name="executorSupplier"> /// The supplier for tasks. /// </param> /// <param name="pooledByteBufferFactory"> /// The factory that allocates IPooledByteBuffer memory. /// </param> /// <param name="bitmapMemoryCache"> /// The memory cache for CloseableImage. /// </param> /// <param name="encodedMemoryCache"> /// The memory cache for IPooledByteBuffer. /// </param> /// <param name="defaultBufferedDiskCache"> /// The default buffered disk cache. /// </param> /// <param name="smallImageBufferedDiskCache"> /// The buffered disk cache used for small images. /// </param> /// <param name="cacheKeyFactory"> /// The factory that creates cache keys for the pipeline. /// </param> /// <param name="platformBitmapFactory"> /// The bitmap factory used for post process. /// </param> /// <param name="flexByteArrayPool"> /// The memory pool used for post process. /// </param> /// <param name="forceSmallCacheThresholdBytes"> /// The threshold set for using the small buffered disk cache. /// </param> public ProducerFactory( IByteArrayPool byteArrayPool, ImageDecoder imageDecoder, IProgressiveJpegConfig progressiveJpegConfig, bool downsampleEnabled, bool resizeAndRotateEnabledForNetwork, IExecutorSupplier executorSupplier, IPooledByteBufferFactory pooledByteBufferFactory, IMemoryCache <ICacheKey, CloseableImage> bitmapMemoryCache, IMemoryCache <ICacheKey, IPooledByteBuffer> encodedMemoryCache, BufferedDiskCache defaultBufferedDiskCache, BufferedDiskCache smallImageBufferedDiskCache, ICacheKeyFactory cacheKeyFactory, PlatformBitmapFactory platformBitmapFactory, FlexByteArrayPool flexByteArrayPool, int forceSmallCacheThresholdBytes) { _forceSmallCacheThresholdBytes = forceSmallCacheThresholdBytes; _byteArrayPool = byteArrayPool; _imageDecoder = imageDecoder; _progressiveJpegConfig = progressiveJpegConfig; _downsampleEnabled = downsampleEnabled; _resizeAndRotateEnabledForNetwork = resizeAndRotateEnabledForNetwork; _executorSupplier = executorSupplier; _pooledByteBufferFactory = pooledByteBufferFactory; _bitmapMemoryCache = bitmapMemoryCache; _encodedMemoryCache = encodedMemoryCache; _defaultBufferedDiskCache = defaultBufferedDiskCache; _smallImageBufferedDiskCache = smallImageBufferedDiskCache; _cacheKeyFactory = cacheKeyFactory; _platformBitmapFactory = platformBitmapFactory; _flexByteArrayPool = flexByteArrayPool; }