protected sealed override void OnExecute() { for (int i = 0; i < this.iterations; ++i) { EffectConfigToken localToken; if (this.token == null) { localToken = null; } else { localToken = (EffectConfigToken)this.token.Clone(); } RenderArgs srcArgs = new RenderArgs(image); RenderArgs dstArgs = new RenderArgs(dst); BackgroundEffectRenderer ber = new BackgroundEffectRenderer(effect, localToken, dstArgs, srcArgs, region, 25 * Processor.LogicalCpuCount, Processor.LogicalCpuCount); ber.Start(); ber.Join(); ber.Dispose(); ber = null; } }
private bool DoEffect(Effect effect, EffectConfigToken token, PdnRegion selectedRegion, PdnRegion regionToRender, Surface originalSurface) { bool oldDirtyValue = AppWorkspace.ActiveDocumentWorkspace.Document.Dirty; bool resetDirtyValue = false; bool returnVal = false; AppWorkspace.ActiveDocumentWorkspace.EnableOutlineAnimation = false; try { using (ProgressDialog aed = new ProgressDialog()) { if (effect.Image != null) { aed.Icon = Utility.ImageToIcon(effect.Image, Utility.TransparentKey); } aed.Opacity = 0.9; aed.Value = 0; aed.Text = effect.Name; aed.Description = string.Format(PdnResources.GetString("Effects.ApplyingDialog.Description"), effect.Name); invalidateTimer.Enabled = true; using (new WaitCursorChanger(AppWorkspace)) { HistoryMemento ha = null; DialogResult result = DialogResult.None; AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar(); AppWorkspace.Widgets.LayerControl.SuspendLayerPreviewUpdates(); try { ManualResetEvent saveEvent = new ManualResetEvent(false); BitmapHistoryMemento bha = null; // perf bug #1445: save this data in a background thread PdnRegion selectedRegionCopy = selectedRegion.Clone(); PaintDotNet.Threading.ThreadPool.Global.QueueUserWorkItem( delegate(object context) { try { ImageResource image; if (effect.Image == null) { image = null; } else { image = ImageResource.FromImage(effect.Image); } bha = new BitmapHistoryMemento(effect.Name, image, this.AppWorkspace.ActiveDocumentWorkspace, this.AppWorkspace.ActiveDocumentWorkspace.ActiveLayerIndex, selectedRegionCopy, originalSurface); } finally { saveEvent.Set(); selectedRegionCopy.Dispose(); selectedRegionCopy = null; } }); BackgroundEffectRenderer ber = new BackgroundEffectRenderer( effect, token, new RenderArgs(((BitmapLayer)AppWorkspace.ActiveDocumentWorkspace.ActiveLayer).Surface), new RenderArgs(originalSurface), regionToRender, tilesPerCpu * renderingThreadCount, renderingThreadCount); aed.Tag = ber; ber.RenderedTile += new RenderedTileEventHandler(aed.RenderedTileHandler); ber.RenderedTile += new RenderedTileEventHandler(RenderedTileHandler); ber.StartingRendering += new EventHandler(StartingRenderingHandler); ber.FinishedRendering += new EventHandler(aed.FinishedRenderingHandler); ber.FinishedRendering += new EventHandler(FinishedRenderingHandler); ber.Start(); result = Utility.ShowDialog(aed, AppWorkspace); if (result == DialogResult.Cancel) { resetDirtyValue = true; using (new WaitCursorChanger(AppWorkspace)) { ber.Abort(); ber.Join(); ((BitmapLayer)AppWorkspace.ActiveDocumentWorkspace.ActiveLayer).Surface.CopySurface(originalSurface); } } invalidateTimer.Enabled = false; ber.Join(); ber.Dispose(); saveEvent.WaitOne(); saveEvent.Close(); saveEvent = null; ha = bha; } catch { using (new WaitCursorChanger(AppWorkspace)) { ((BitmapLayer)AppWorkspace.ActiveDocumentWorkspace.ActiveLayer).Surface.CopySurface(originalSurface); ha = null; } } finally { AppWorkspace.Widgets.LayerControl.ResumeLayerPreviewUpdates(); } using (PdnRegion simplifiedRenderRegion = Utility.SimplifyAndInflateRegion(selectedRegion)) { using (new WaitCursorChanger(AppWorkspace)) { AppWorkspace.ActiveDocumentWorkspace.ActiveLayer.Invalidate(simplifiedRenderRegion); } } using (new WaitCursorChanger(AppWorkspace)) { if (result == DialogResult.OK) { if (ha != null) { AppWorkspace.ActiveDocumentWorkspace.History.PushNewMemento(ha); } AppWorkspace.Update(); returnVal = true; } else { Utility.GCFullCollect(); } } } // using } // using } finally { AppWorkspace.ActiveDocumentWorkspace.EnableOutlineAnimation = true; if (resetDirtyValue) { AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = oldDirtyValue; } } AppWorkspace.Widgets.StatusBarProgress.EraseProgressStatusBarAsync(); return returnVal; }