コード例 #1
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            if (historyWorkspace.Selection.IsEmpty)
            {
                return(null);
            }
            SelectionHistoryMemento             memento      = new SelectionHistoryMemento(StaticName, StaticImage, historyWorkspace);
            GeometryList                        selectedPath = historyWorkspace.Selection.GetCachedGeometryList();
            SelectionRenderingQuality           selectionRenderingQuality = historyWorkspace.ToolSettings.Selection.RenderingQuality.Value;
            Result <IReadOnlyList <RectInt32> > selectedPathScansLazy     = historyWorkspace.Selection.GetCachedLazyClippingMaskScans();
            RectInt32           documentBounds = historyWorkspace.Document.Bounds();
            Func <GeometryList> invertedPathFn = delegate {
                if ((selectionRenderingQuality == SelectionRenderingQuality.Aliased) || selectedPath.IsPixelated)
                {
                    GeometryList list2 = GeometryList.FromNonOverlappingSortedScans(selectedPathScansLazy.Value);
                    list2.AddRect(documentBounds);
                    SegmentedList <RectInt32> scans = new SegmentedList <RectInt32>();
                    foreach (RectInt32 num in list2.EnumerateInteriorScans())
                    {
                        if (documentBounds.Contains(num))
                        {
                            scans.Add(num);
                        }
                        else if (documentBounds.IntersectsWith(num))
                        {
                            scans.Add(RectInt32.Intersect(documentBounds, num));
                        }
                    }
                    return(GeometryList.FromNonOverlappingScans(scans));
                }
                GeometryList lhs = documentBounds.Contains(selectedPath.Bounds) ? selectedPath : GeometryList.ClipToRect(selectedPath, documentBounds);
                return(GeometryList.Combine(lhs, GeometryCombineMode.Xor, documentBounds));
            };
            ThreadTask <GeometryList> task = historyWorkspace.TaskManager.StartNewThreadTask <GeometryList>(task => invertedPathFn(), ApartmentState.MTA);
            ManualResetEvent          taskFinishedEvent = new ManualResetEvent(false);

            task.ResultAsync <GeometryList>().Receive(delegate(Result <GeometryList> r) {
                taskFinishedEvent.Set();
            }).Observe();
            if (!taskFinishedEvent.WaitOne(0x3e8))
            {
                using (TaskProgressDialog dialog = new TaskProgressDialog())
                {
                    dialog.Task       = task;
                    dialog.Text       = StaticName;
                    dialog.Icon       = StaticImage.Reference.ToIcon();
                    dialog.HeaderText = PdnResources.GetString("SaveConfigDialog.Finishing.Text");
                    dialog.ShowDialog(historyWorkspace.Window);
                }
            }
            Result <GeometryList> taskResult = task.TaskResult;

            if (taskResult.IsError)
            {
                if (taskResult.Error is OutOfMemoryException)
                {
                    throw new OutOfMemoryException(null, taskResult.Error);
                }
                throw new AggregateException(null, taskResult.Error);
            }
            GeometryList geometry = task.TaskResult.Value;

            base.EnterCriticalRegion();
            using (historyWorkspace.Selection.UseChangeScope())
            {
                historyWorkspace.Selection.Reset();
                geometry.Freeze();
                historyWorkspace.Selection.SetContinuation(geometry, SelectionCombineMode.Replace);
                historyWorkspace.Selection.CommitContinuation();
            }
            return(memento);
        }
コード例 #2
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            historyWorkspace.VerifyAccess();
            BitmapLayer       layer               = (BitmapLayer)historyWorkspace.Document.Layers[this.layerIndex];
            Surface           layerSurface        = layer.Surface;
            TileMathHelper    tileMathHelper      = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2);
            TileMathHelper    tileGroupMathHelper = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2 + 3);
            HistoryMemento    actionHM            = null;
            ManualResetEvent  taskFinishedEvent   = new ManualResetEvent(false);
            ThreadTask <Unit> commitTask          = historyWorkspace.TaskManager.StartNewThreadTask(delegate(Task task) {
                SegmentedList <RectInt32> list = tileGroupMathHelper.EnumerateTilesClippedToSourceRect(this.rendererClipRect).ToSegmentedList <RectInt32>();
                ListUtil.FisherYatesShuffle <RectInt32>(list);
                ConcurrentQueue <ISurface <ColorBgra> > queue = new ConcurrentQueue <ISurface <ColorBgra> >();
                ConcurrentQueue <RectInt32> changedTiles      = new ConcurrentQueue <RectInt32>();
                ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > > changedTileGroups = new ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > >();
                double progressIncrement = 1.0 / ((double)Math.Max(1, list.Count));
                task.Progress            = 0.0;
                Work.ParallelForEach <RectInt32>(WaitType.Blocking, list, delegate(RectInt32 tileGroupRect) {
                    ISurface <ColorBgra> dst = RetryManager.RunMemorySensitiveOperation <ISurfaceRef <ColorBgra> >(() => SurfaceAllocator.Bgra.Allocate <ColorBgra>(tileGroupRect.Size, AllocationOptions.ZeroFillNotRequired));
                    this.renderer.Render(dst, tileGroupRect.Location);
                    bool flag = false;
                    foreach (RectInt32 num in tileMathHelper.EnumerateTilesClippedToSourceRect(tileGroupRect))
                    {
                        RectInt32 bounds = RectInt32.Offset(num, -tileGroupRect.Location);
                        using (ISurface <ColorBgra> surface2 = layerSurface.CreateWindow(num))
                        {
                            using (ISurface <ColorBgra> surface3 = dst.CreateWindow <ColorBgra>(bounds))
                            {
                                if (!SurfaceBgraUtil.ArePixelsEqual(surface2, surface3))
                                {
                                    flag = true;
                                    changedTiles.Enqueue(num);
                                }
                            }
                        }
                    }
                    if (flag)
                    {
                        changedTileGroups.Enqueue(TupleStruct.Create <RectInt32, ISurface <ColorBgra> >(tileGroupRect, dst));
                    }
                    else
                    {
                        DisposableUtil.Free <ISurface <ColorBgra> >(ref dst);
                    }
                    task.IncrementProgressBy(progressIncrement);
                }, WorkItemQueuePriority.Normal, null);
                task.Progress = null;
                if (changedTiles.Count == 0)
                {
                    actionHM = null;
                }
                else
                {
                    SegmentedList <RectInt32> scans = new SegmentedList <RectInt32>(changedTiles.Count, 7);
                    scans.AddRange(changedTiles);
                    ScansHelpers.SortScansByTopLeft(scans);
                    ScansHelpers.ConsolidateSortedScansInPlace(scans);
                    actionHM = new BitmapHistoryMemento(this.historyMementoName, this.historyMementoImage, historyWorkspace, this.layerIndex, scans);
                    this.EnterCriticalRegion();
                    Work.ParallelForEach <TupleStruct <RectInt32, ISurface <ColorBgra> > >(WaitType.Blocking, changedTileGroups, delegate(TupleStruct <RectInt32, ISurface <ColorBgra> > tileInfo) {
                        using (ISurface <ColorBgra> surface = layerSurface.CreateWindow(tileInfo.Item1))
                        {
                            tileInfo.Item2.Render(surface, PointInt32.Zero);
                        }
                        tileInfo.Item2.Dispose();
                    }, WorkItemQueuePriority.Normal, null);
                    foreach (RectInt32 num in scans)
                    {
                        layer.Invalidate(num);
                    }
                }
            }, ApartmentState.MTA);

            commitTask.ResultAsync <Unit>().Receive(delegate(Result <Unit> r) {
                taskFinishedEvent.Set();
            }).Observe();
            if (!taskFinishedEvent.WaitOne(this.delayUntilProgressDialogMs))
            {
                string headerText       = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText");
                string headerTextFormat = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText.Format");
                using (TaskProgressDialog progressDialog = new TaskProgressDialog())
                {
                    Action updateHeaderText = delegate {
                        progressDialog.VerifyAccess();
                        if (!progressDialog.IsDisposed)
                        {
                            string text1;
                            double?progress = commitTask.Progress;
                            if (!progress.HasValue)
                            {
                                text1 = headerText;
                            }
                            else
                            {
                                text1 = string.Format(headerTextFormat, (progress.Value * 100.0).ToString("N0"));
                            }
                            progressDialog.HeaderText = text1;
                        }
                    };
                    progressDialog.Text             = this.historyMementoName;
                    progressDialog.Icon             = this.historyMementoImage.Reference.ToIcon();
                    progressDialog.CloseOnFinished  = true;
                    progressDialog.ShowCancelButton = false;
                    progressDialog.Task             = commitTask;
                    updateHeaderText();
                    commitTask.ProgressChanged += delegate(object s, ValueEventArgs <double?> e) {
                        PdnSynchronizationContext.Instance.EnsurePosted(updateHeaderText);
                    };
                    progressDialog.ShowDialog(historyWorkspace.Window);
                }
            }
            if (!commitTask.TaskResult.IsError)
            {
                return(actionHM);
            }
            if (commitTask.TaskResult.Error is OutOfMemoryException)
            {
                throw new OutOfMemoryException(null, commitTask.TaskResult.Error);
            }
            throw new AggregateException(null, commitTask.TaskResult.Error);
        }