protected override Task <SharedEvasImage> GenerateImageAsync(string path, ImageSource source, Stream imageData, ImageInformation imageInformation, bool enableTransformations, bool isPlaceholder)
        {
            if (imageData == null)
            {
                throw new ArgumentNullException(nameof(imageData));
            }

            ThrowIfCancellationRequested();

            TaskCompletionSource <SharedEvasImage> tcs = new TaskCompletionSource <SharedEvasImage>();

            MainThreadDispatcher.PostAsync(() =>
            {
                SharedEvasImage img = new SharedEvasImage(MainWindow);
                img.IsFilled        = true;
                img.Show();
                img.SetStream(imageData);
                imageData.TryDispose();

                img.AddRef();
                EcoreMainloop.AddTimer(1.0, () => {
                    img.RemoveRef();
                    return(false);
                });

                imageInformation.SetOriginalSize(img.Size.Width, img.Size.Height);
                imageInformation.SetCurrentSize(img.Size.Width, img.Size.Height);

                // DOWNSAMPLE
                if (Parameters.DownSampleSize != null && (Parameters.DownSampleSize.Item1 > 0 || Parameters.DownSampleSize.Item2 > 0))
                {
                    // Calculate inSampleSize
                    int downsampleWidth  = Parameters.DownSampleSize.Item1;
                    int downsampleHeight = Parameters.DownSampleSize.Item2;

                    if (Parameters.DownSampleUseDipUnits)
                    {
                        downsampleWidth  = DpiToPixels(downsampleWidth);
                        downsampleHeight = DpiToPixels(downsampleHeight);
                    }

                    int scaleDownFactor = CalculateScaleDownFactor(img.Size.Width, img.Size.Height, downsampleWidth, downsampleHeight);

                    if (scaleDownFactor > 1)
                    {
                        //System.//Console.WriteLine("GenerateImageAsync:: DownSample with {0}", scaleDownFactor);
                        imageInformation.SetCurrentSize(
                            (int)((double)img.Size.Width / scaleDownFactor),
                            (int)((double)img.Size.Height / scaleDownFactor));
                        EvasInterop.evas_object_image_load_scale_down_set(img.RealHandle, scaleDownFactor);
                    }
                }
                tcs.SetResult(img);
            });
            return(tcs.Task);
        }
        protected override async Task SetTargetAsync(SharedEvasImage image, bool animated)
        {
            if (Target == null)
            {
                return;
            }

            await MainThreadDispatcher.PostAsync(() =>
            {
                ThrowIfCancellationRequested();
                PlatformTarget.Set(this, image, animated);
            }).ConfigureAwait(false);
        }
        protected override Task SetTargetAsync(SharedEvasImage image, bool animated)
        {
            if (Target == null)
            {
                return(Task.FromResult(true));
            }

            return(MainThreadDispatcher.PostAsync(() =>
            {
                ThrowIfCancellationRequested();
                PlatformTarget.Set(this, image, animated);
            }));
        }
        protected override async Task <SharedEvasImage> TransformAsync(SharedEvasImage bitmap, IList <ITransformation> transformations, string path, ImageSource source, bool isPlaceholder)
        {
            await StaticLocks.DecodingLock.WaitAsync(CancellationTokenSource.Token).ConfigureAwait(false); // Applying transformations is both CPU and memory intensive

            ThrowIfCancellationRequested();

            try
            {
                foreach (var transformation in transformations)
                {
                    ThrowIfCancellationRequested();

                    var old = bitmap;

                    try
                    {
                        var bitmapHolder = transformation.Transform(new BitmapHolder(bitmap), path, source, isPlaceholder, Key);
                        bitmap = bitmapHolder.ToNative();
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(string.Format("Transformation failed: {0}", transformation.Key), ex);
                        throw;
                    }
                    finally
                    {
                        // Transformation succeeded, so garbage the source
                        if (old != null && old.Handle != IntPtr.Zero && old != bitmap && old.Handle != bitmap.Handle)
                        {
                            //TODO Is it neccessary?
                            //old.DisposeOnMainThread();
                        }
                    }
                }
            }
            finally
            {
                StaticLocks.DecodingLock.Release();
            }

            return(bitmap);
        }
Ejemplo n.º 5
0
 public BitmapHolder(SharedEvasImage bitmap)
 {
     NativeBitmap = bitmap;
 }