public BrushStrokeRenderCache(BrushStrokeRenderData renderData, BrushStamp stamp, int tileEdgeLog2) { Validate.Begin().IsNotNull <BrushStrokeRenderData>(renderData, "renderData").IsNotNull <BrushStamp>(stamp, "stamp").Check(); this.renderData = renderData; this.stamp = stamp; this.lazyStampMaskDevBitmap = LazyResult.New <DeviceBitmap>(() => new DeviceBitmap(this.stamp.MaskBitmap).EnsureFrozen <DeviceBitmap>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection()); this.tileOffsetToTileDataMap = new ConcurrentDictionary <PointInt32, TileData>(); this.tileMathHelper = new TileMathHelper(TransactedToolChanges.MaxMaxRenderBounds.Size, tileEdgeLog2); }
public DocumentCanvasLayerViewMipLayer(DocumentCanvasLayerView owner, int canvasTileEdgeLog2, int mipLevel, DocumentCanvasTileCache tileCache) { Validate.Begin().IsNotNull <DocumentCanvasLayerView>(owner, "owner").IsNotNull <DocumentCanvasTileCache>(tileCache, "tileCache").Check().IsGreaterThanOrEqualTo(canvasTileEdgeLog2, 1, "canvasTileEdgeLog2").Check(); owner.VerifyAccess(); this.owner = owner; this.tileCache = tileCache; this.canvasView = this.owner.CanvasView; this.canvasView.ViewportCanvasBoundsChanged += new ValueChangedEventHandler <RectDouble>(this.OnCanvasViewViewportCanvasBoundsChanged); this.mipLevel = mipLevel; this.tileMathHelper = this.tileCache.TileMathHelper; this.deviceBitmapTickets = ArrayUtil.Create2D <ObjectPoolTicket <IDeviceBitmap> >(this.tileMathHelper.TileRows, this.tileMathHelper.TileColumns); this.deviceBitmaps = ArrayUtil.Create2D <IDeviceBitmap>(this.tileMathHelper.TileRows, this.tileMathHelper.TileColumns); this.tileBuffers = ArrayUtil.Create2D <IBitmap <ColorPbgra32> >(this.tileMathHelper.TileRows, this.tileMathHelper.TileColumns); this.isDeviceBitmapCurrent = ArrayUtil.Create2D <bool>(this.tileMathHelper.TileRows, this.tileMathHelper.TileColumns); }
private static int SaveToFile(Tile[] map, ObjectTile[] objects, string fileName, int columnCount) { int counter = -1; List <string> lines = new List <string>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < map.Length; i++) { int mod = (i % columnCount); if (mod == 0) { counter++; if (sb.Length > 0) { lines.Add(sb.ToString()); sb.Clear(); } } sb.Append(Convert.ToInt32(map[(counter * columnCount) + mod].Id.ToString())); } if (sb.Length > 0) { lines.Add(sb.ToString()); } if (objects != null) { foreach (ObjectTile item in objects) { int row, column; TileMathHelper.ToPosition(item.TileIndex, columnCount, out row, out column); lines[row] = lines[row].Remove(column, 1).Insert(column, item.ObjectId.ToString()); } } System.IO.File.WriteAllLines(fileName + ".txt", lines.ToArray()); return(map.Length); }
protected override GeometryList CreateSelectionGeometry(MagicWandToolChanges changes, AsyncSelectionToolCreateGeometryContext context, CancellationToken cancellationToken) { GeometryList list; Result <BitVector2D> lazyBaseStencil; IRenderer <ColorBgra> sampleSource = ((MagicWandToolCreateGeometryContext)context).SampleSource; byte x = (byte)Math.Round((double)(changes.Tolerance * 255.0), MidpointRounding.AwayFromZero); byte tolerance = ByteUtil.FastScale(x, x); PointInt32 pt = changes.OriginPointInt32; if (!sampleSource.Bounds <ColorBgra>().Contains(pt)) { switch (changes.SelectionCombineMode) { case SelectionCombineMode.Replace: case SelectionCombineMode.Intersect: return(new GeometryList()); case SelectionCombineMode.Union: case SelectionCombineMode.Exclude: case SelectionCombineMode.Xor: return(changes.BaseGeometry); } throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode"); } if (cancellationToken.IsCancellationRequested) { return(null); } Func <bool> isCancellationRequestedFn = () => cancellationToken.IsCancellationRequested; ColorBgra basis = sampleSource.GetPointSlow(pt); int width = ((sampleSource.Width + 0x1f) / 0x20) * 0x20; BitVector2D newStencil = new BitVector2D(width, sampleSource.Height); BitVector2DStruct newStencilWrapper = new BitVector2DStruct(newStencil); if (((changes.SelectionCombineMode != SelectionCombineMode.Replace) && sampleSource.Bounds <ColorBgra>().Contains(changes.BaseGeometry.Bounds.Int32Bound)) && changes.BaseGeometry.IsPixelated) { lazyBaseStencil = LazyResult.New <BitVector2D>(() => PixelatedGeometryListToBitVector2D(changes.BaseGeometry, newStencil.Width, newStencil.Height, cancellationToken), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection()); ThreadPool.QueueUserWorkItem(delegate(object _) { lazyBaseStencil.EnsureEvaluated(); }); } else { lazyBaseStencil = null; } FloodMode floodMode = changes.FloodMode; if (floodMode != FloodMode.Local) { if (floodMode != FloodMode.Global) { throw ExceptionUtil.InvalidEnumArgumentException <FloodMode>(changes.FloodMode, "changes.FloodMode"); } } else { RectInt32 num4; FloodFillAlgorithm.FillStencilFromPoint <BitVector2DStruct>(sampleSource, newStencilWrapper, pt, tolerance, isCancellationRequestedFn, out num4); goto Label_0293; } TileMathHelper tileMathHelper = new TileMathHelper(sampleSource.Width, sampleSource.Height, 7); Work.ParallelForEach <PointInt32>(WaitType.Pumping, tileMathHelper.EnumerateTileOffsets(), delegate(PointInt32 tileOffset) { if (!cancellationToken.IsCancellationRequested) { RectInt32 clipRect = tileMathHelper.GetTileSourceRect(tileOffset); FloodFillAlgorithm.FillStencilByColor <BitVector2DStruct>(sampleSource, newStencilWrapper, basis, tolerance, isCancellationRequestedFn, clipRect); } }, WorkItemQueuePriority.Normal, null); Label_0293: if (cancellationToken.IsCancellationRequested) { return(null); } if (changes.SelectionCombineMode == SelectionCombineMode.Replace) { list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken); } else if (lazyBaseStencil == null) { GeometryList rhs = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken); if (cancellationToken.IsCancellationRequested) { return(null); } list = GeometryList.Combine(changes.BaseGeometry, changes.SelectionCombineMode.ToGeometryCombineMode(), rhs); } else { BitVector2D other = lazyBaseStencil.Value; if (cancellationToken.IsCancellationRequested) { return(null); } switch (changes.SelectionCombineMode) { case SelectionCombineMode.Replace: throw new InternalErrorException(); case SelectionCombineMode.Union: newStencil.Or(other); break; case SelectionCombineMode.Exclude: newStencil.Invert(); if (cancellationToken.IsCancellationRequested) { return(null); } newStencil.And(other); break; case SelectionCombineMode.Intersect: newStencil.And(other); break; case SelectionCombineMode.Xor: newStencil.Xor(other); break; default: throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode"); } if (cancellationToken.IsCancellationRequested) { return(null); } list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken); } if (cancellationToken.IsCancellationRequested) { return(null); } list.Freeze(); return(list); }
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); }