コード例 #1
0
ファイル: ThumbnailManager.cs プロジェクト: ykafia/Paint.Net4
        private bool RenderThreadLoop()
        {
            Tuple <IThumbnailProvider, ValueEventHandler <Tuple <IThumbnailProvider, ISurface <ColorBgra> > >, int> tuple = null;
            object updateLock = this.updateLock;

            lock (updateLock)
            {
                if (!this.quitRenderThread)
                {
                    goto Label_0043;
                }
                return(false);

Label_0025:
                Monitor.Wait(this.updateLock);
                if (this.quitRenderThread)
                {
                    return(false);
                }
Label_0043:
                if (this.renderQueue.Count == 0)
                {
                    goto Label_0025;
                }
                this.renderingInactive.Reset();
                while ((this.renderQueue.Count > 0) && (this.renderQueue.Peek() == null))
                {
                    this.renderQueue.Dequeue();
                }
                if (this.renderQueue.Count > 0)
                {
                    tuple = this.renderQueue.Dequeue();
                }
            }
            if (tuple != null)
            {
                Thread.Sleep(this.updateLatency);
            }
            bool   flag = true;
            object obj3 = this.updateLock;

            lock (obj3)
            {
                if (this.quitRenderThread)
                {
                    return(false);
                }
                if (((this.renderQueue.Count > 0) && (tuple != null)) && tuple.Equals(this.renderQueue.Peek()))
                {
                    flag = false;
                }
            }
            if (tuple == null)
            {
                flag = false;
            }
            if (flag)
            {
                try
                {
                    ISurface <ColorBgra> surface;
                    Thread.Sleep(this.updateLatency);
                    using (IRenderer <ColorBgra> renderer = tuple.Item1.CreateThumbnailRenderer(tuple.Item3))
                    {
                        surface = renderer.ToCancellable <ColorBgra>(CancellationTokenUtil.Create((Func <bool>)(() => this.quitRenderThread))).Parallelize <ColorBgra>(TilingStrategy.HorizontalSlices, 0, WorkItemQueuePriority.Lowest).ToSurface();
                    }
                    bool   flag5 = false;
                    object obj4  = this.updateLock;
                    lock (obj4)
                    {
                        if (this.quitRenderThread)
                        {
                            surface.Dispose();
                            surface = null;
                            return(false);
                        }
                        if ((this.renderQueue.Count > 0) && tuple.Equals(this.renderQueue.Peek()))
                        {
                            flag5 = true;
                        }
                    }
                    if (!flag5)
                    {
                        flag5 = !this.OnThumbnailReady(tuple.Item1, tuple.Item2, surface);
                    }
                    if (flag5)
                    {
                        surface.Dispose();
                    }
                }
                catch (Exception exception)
                {
                    if (!(exception is OperationCanceledException))
                    {
                        AggregateException exception2 = exception as AggregateException;
                    }
                }
            }
            this.renderingInactive.Set();
            return(true);
        }
コード例 #2
0
 public static void FillStencilByColor <TBitVector2D>(IRenderer <ColorBgra> sampleSource, TBitVector2D stencilBuffer, ColorBgra basis, byte tolerance, CancellationToken cancelToken, RectInt32 clipRect) where TBitVector2D : IBitVector2D
 {
     FillStencilByColor <TBitVector2D>(sampleSource, stencilBuffer, basis, tolerance, CancellationTokenUtil.Create(cancelToken), clipRect);
 }
コード例 #3
0
        private unsafe void RenderTileWorkItem(PointInt32 tileOffset)
        {
            IBitmap <ColorPbgra32> bitmap;
            bool      isCancelled = false;
            bool      flag        = false;
            Exception error       = null;

            isCancelled |= this.IsTileRenderingCancelled(tileOffset);
            if (isCancelled)
            {
                bitmap = null;
            }
            else
            {
                RectInt32 tileSourceRect = this.tileMathHelper.GetTileSourceRect(tileOffset);
                SizeInt32 tileBufferSize = this.GetTileBufferSize(tileOffset);
                bitmap = RetryManager.Eval <IBitmap <ColorPbgra32> >(3, () => BitmapAllocator.Pbgra32.Allocate(tileBufferSize, AllocationOptions.Default), delegate(Exception _) {
                    CleanupManager.RequestCleanup();
                    Thread.Sleep(200);
                    CleanupManager.WaitForPendingCleanup();
                }, delegate(AggregateException ex) {
                    throw new AggregateException($"could not allocate a bitmap of size {tileBufferSize.Width} x {tileBufferSize.Height}", ex).Flatten();
                });
                if (this.source != null)
                {
                    try
                    {
                        isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                        if (!isCancelled)
                        {
                            using (IBitmapLock <ColorPbgra32> @lock = bitmap.Lock <ColorPbgra32>(BitmapLockOptions.ReadWrite))
                            {
                                if (this.mipLevel == 0)
                                {
                                    this.source.CopyPixels(new RectInt32?(tileSourceRect), @lock);
                                    RenderingKernels.ConvertBgra32ToPbgra32((uint *)@lock.Scan0, tileBufferSize.Width, tileBufferSize.Height, @lock.Stride);
                                    flag = true;
                                }
                                else
                                {
                                    BitmapInterpolationMode linear;
                                    if (!this.isHighQuality)
                                    {
                                        linear = BitmapInterpolationMode.Linear;
                                    }
                                    else if (this.mipLevel == 1)
                                    {
                                        linear = BitmapInterpolationMode.Linear;
                                    }
                                    else
                                    {
                                        linear = BitmapInterpolationMode.Fant;
                                    }
                                    IImagingFactory    instance    = ImagingFactory.Instance;
                                    ICancellationToken cancelToken = CancellationTokenUtil.Create((Func <bool>)(() => (isCancelled | this.IsTileRenderingCancelled(tileOffset))));
                                    int copyHeightLog2             = Math.Max(3, 7 - this.mipLevel);
                                    using (ClippedBitmapSource <ColorBgra32> source2 = new ClippedBitmapSource <ColorBgra32>(this.source, tileSourceRect))
                                    {
                                        using (CancellableBitmapSource <ColorBgra32> source3 = new CancellableBitmapSource <ColorBgra32>(source2, r => this.tileMathHelper.EnumerateTilesClippedToSourceRect(r), null, cancelToken))
                                        {
                                            using (IBitmapSource <ColorPbgra32> source4 = CreateBufferedTileScaler(instance, source3, tileBufferSize.Width, tileBufferSize.Height, linear))
                                            {
                                                using (CancellableBitmapSource <ColorPbgra32> source5 = new CancellableBitmapSource <ColorPbgra32>(source4, r => TileRectSplitter(r, ((int)1) << copyHeightLog2), null, cancelToken))
                                                {
                                                    try
                                                    {
                                                        source5.CopyPixels <ColorPbgra32>(@lock);
                                                        flag = true;
                                                    }
                                                    catch (OperationCanceledException exception2)
                                                    {
                                                        error       = exception2;
                                                        isCancelled = true;
                                                    }
                                                    catch (Exception exception3)
                                                    {
                                                        error = exception3;
                                                        throw;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                                if (isCancelled)
                                {
                                    flag = false;
                                }
                            }
                            if (!flag)
                            {
                                DisposableUtil.Free <IBitmap <ColorPbgra32> >(ref bitmap);
                            }
                        }
                    }
                    catch (OperationCanceledException exception4)
                    {
                        error       = exception4;
                        isCancelled = true;
                    }
                    catch (Exception exception5)
                    {
                        error        = exception5;
                        isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                        if (!isCancelled)
                        {
                            using (IDrawingContext context = DrawingContext.FromBitmap(bitmap, FactorySource.PerThread))
                            {
                                context.Clear(new ColorRgba128Float?((ColorRgba128Float)Colors.White));
                                string text = exception5.ToString();
                                using (ISystemFonts fonts = new SystemFonts(true))
                                {
                                    TextLayout textLayout = UIText.CreateLayout(context, text, fonts.Caption, null, HotkeyRenderMode.Ignore, (double)bitmap.Size.Width, 65535.0);
                                    textLayout.FontSize    *= 0.6;
                                    textLayout.WordWrapping = WordWrapping.Wrap;
                                    context.DrawTextLayout(PointDouble.Zero, textLayout, SolidColorBrushCache.Get((ColorRgba128Float)Colors.Black), DrawTextOptions.None);
                                }
                            }
                            flag = true;
                        }
                    }
                }
            }
            isCancelled |= this.IsTileRenderingCancelled(tileOffset);
            if (isCancelled)
            {
                DisposableUtil.Free <IBitmap <ColorPbgra32> >(ref bitmap);
            }
            RenderedTileInfo info = new RenderedTileInfo(bitmap, !isCancelled && (bitmap > null), error);

            if (!this.tilesRenderedQueue.TryEnqueue(tileOffset, info))
            {
                ExceptionUtil.ThrowInternalErrorException("Could not enqueue to this.tilesRenderedQueue");
            }
            if (Interlocked.Exchange(ref this.isProcessTileRenderedQueueQueued, 1) == 0)
            {
                this.syncContext.Post(this.processTileRenderedQueueCallback);
            }
        }
コード例 #4
0
 public static void FillStencilFromPoint <TBitVector2D>(IRenderer <ColorBgra> sampleSource, TBitVector2D stencilBuffer, PointInt32 startPt, byte tolerance, CancellationToken cancelToken, out RectInt32 bounds) where TBitVector2D : IBitVector2D
 {
     FillStencilFromPoint <TBitVector2D>(sampleSource, stencilBuffer, startPt, tolerance, CancellationTokenUtil.Create(cancelToken), out bounds);
 }