Browse Source

Another small performance boosts

Locked and unlocked bitmaps
flabbet 5 years ago
parent
commit
27089bd5b1

+ 1 - 1
PixiEditorDotNetCore3/Models/Tools/Tools/LineTool.cs

@@ -16,7 +16,7 @@ namespace PixiEditorDotNetCore3.Models.Tools.Tools
         }
 
         public void CreateLine(Layer layer, Coordinates coordinates, Color color, int size)
-        {           
+        {
             layer.LayerBitmap.DrawLineBresenham(coordinates.X, coordinates.Y, MousePositionConverter.CurrentCoordinates.X,
                 MousePositionConverter.CurrentCoordinates.Y, color);
         }

+ 6 - 3
PixiEditorDotNetCore3/Models/Tools/ToolsManager.cs

@@ -42,18 +42,21 @@ namespace PixiEditorDotNetCore3.Models.Tools
         private void LoopTimer_Elapsed(object sender, ElapsedEventArgs e)
         {
             Application.Current.Dispatcher.Invoke(() =>
-            {              
-                if(_clonedBitmap != null)
+            {           
+                _layer.LayerBitmap.Lock();
+                if (_clonedBitmap != null)
                 {
                     _layer.LayerBitmap.Clear();
                     _layer.LayerBitmap.Blit(new Rect(new Size(_layer.Width, _layer.Height)), _clonedBitmap, new Rect(new Size(_layer.Width, _layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
-                }
+                }                
                 BitmapPixelChanges changes = SelectedTool.Use(_layer, _startCoordinates, _color, _toolSzie);
 
                 if (!SelectedTool.ExecutesItself)
                 {
                     _layer.ApplyPixels(changes, changes.PixelsColor);
                 }
+                _layer.LayerBitmap.Unlock();
+
             });
         }
 

+ 5 - 0
PixiEditorTests/PerformanceTests/BitmapOperationsTests.cs

@@ -18,10 +18,15 @@ namespace PixiEditorTests.PerformanceTests
         public void FillBitmapWithPixelsTest(int width, int height)
         {
             WriteableBitmap bitmap = BitmapFactory.New(width, height);
+            bitmap.Lock();
+
             for (int i = 0; i < width * height; i++)
             {
                 bitmap.SetPixeli(i, 0xFFFFF);
+
             }
+            bitmap.Unlock();
+
             Assert.Pass();
         }
     }