Skip to content

Commit 556760c

Browse files
Optimalization (#263)
* Change DispatcherPriority for Invalidation; Speed up Layer rendering * SkiaUtils::SetPixel SKBitmap::SetPixel usage replaced with optimized alternative; Pen drawing optimized * TODO * Remove ClipToBounds from TransparentBackground * Remove redundant properties from PaintCanvasImageControl * Move PaintCanvas related classes to PaintCanvas namespace
1 parent 90121b1 commit 556760c

22 files changed

+112
-106
lines changed

Pixed.Application/Controls/ImageGrid.cs renamed to Pixed.Application/Controls/PaintCanvas/GridOverlay.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
using System;
66

77

8-
namespace Pixed.Application.Controls;
9-
internal class ImageGrid : OverlayControl
8+
namespace Pixed.Application.Controls.PaintCanvas;
9+
internal class GridOverlay : OverlayControl
1010
{
1111
public const int MinGridSize = 15;
12-
public ImageGrid()
12+
public GridOverlay()
1313
{
1414
ClipToBounds = false;
1515
}

Pixed.Application/Controls/OverlayControl.cs renamed to Pixed.Application/Controls/PaintCanvas/OverlayControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Pixed.Application.Zoom;
99
using SkiaSharp;
1010

11-
namespace Pixed.Application.Controls;
11+
namespace Pixed.Application.Controls.PaintCanvas;
1212
internal abstract class OverlayControl : Control
1313
{
1414
class DrawOperation(Rect bounds, OverlayControl instance) : ICustomDrawOperation

Pixed.Application/Controls/PaintCanvasImageControl.cs renamed to Pixed.Application/Controls/PaintCanvas/PaintContainer.cs

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,25 @@
44
using Avalonia.Controls;
55
using Avalonia.Media;
66
using Avalonia.Metadata;
7-
using Avalonia.Threading;
87
using Pixed.Core.Models;
8+
using SkiaSharp;
99

10-
namespace Pixed.Application.Controls;
10+
namespace Pixed.Application.Controls.PaintCanvas;
1111

12-
internal class PaintCanvasImageControl : Control
12+
internal class PaintContainer : Control
1313
{
1414
/// <summary>
1515
/// Defines the <see cref="Source"/> property.
1616
/// </summary>
1717
public static readonly StyledProperty<PixelImage?> SourceProperty =
18-
AvaloniaProperty.Register<PaintCanvasImageControl, PixelImage?>(nameof(Source));
18+
AvaloniaProperty.Register<PaintContainer, PixelImage?>(nameof(Source));
1919

20-
/// <summary>
21-
/// Defines the <see cref="Stretch"/> property.
22-
/// </summary>
23-
public static readonly StyledProperty<Stretch> StretchProperty =
24-
AvaloniaProperty.Register<PaintCanvasImageControl, Stretch>(nameof(Stretch), Stretch.Uniform);
25-
26-
/// <summary>
27-
/// Defines the <see cref="StretchDirection"/> property.
28-
/// </summary>
29-
public static readonly StyledProperty<StretchDirection> StretchDirectionProperty =
30-
AvaloniaProperty.Register<PaintCanvasImageControl, StretchDirection>(
31-
nameof(StretchDirection),
32-
StretchDirection.Both);
3320

34-
static PaintCanvasImageControl()
21+
static PaintContainer()
3522
{
36-
AffectsRender<PaintCanvasImageControl>(SourceProperty, StretchProperty, StretchDirectionProperty);
37-
AffectsMeasure<PaintCanvasImageControl>(SourceProperty, StretchProperty, StretchDirectionProperty);
38-
AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<PaintCanvasImageControl>(AutomationControlType.Image);
23+
AffectsRender<PaintContainer>(SourceProperty);
24+
AffectsMeasure<PaintContainer>(SourceProperty);
25+
AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<PaintContainer>(AutomationControlType.Image);
3926
}
4027

4128
private readonly PixelDrawOperation _image = new();
@@ -50,24 +37,6 @@ public PixelImage? Source
5037
set => SetValue(SourceProperty, value);
5138
}
5239

53-
/// <summary>
54-
/// Gets or sets a value controlling how the image will be stretched.
55-
/// </summary>
56-
public Stretch Stretch
57-
{
58-
get => GetValue(StretchProperty);
59-
set => SetValue(StretchProperty, value);
60-
}
61-
62-
/// <summary>
63-
/// Gets or sets a value controlling in what direction the image will be stretched.
64-
/// </summary>
65-
public StretchDirection StretchDirection
66-
{
67-
get => GetValue(StretchDirectionProperty);
68-
set => SetValue(StretchDirectionProperty, value);
69-
}
70-
7140
/// <inheritdoc />
7241
protected override bool BypassFlowDirectionPolicies => true;
7342

@@ -97,8 +66,6 @@ public sealed override void Render(DrawingContext context)
9766
_image.Bounds = sourceRect;
9867
context.DrawImage(_image, sourceRect, sourceRect);
9968
}
100-
101-
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Input);
10269
}
10370

10471
/// <summary>
@@ -113,7 +80,7 @@ protected override Size MeasureOverride(Size availableSize)
11380

11481
if (source != null)
11582
{
116-
result = Stretch.CalculateSize(availableSize, new Size(source.Width, source.Height), StretchDirection);
83+
result = Stretch.Uniform.CalculateSize(availableSize, new Size(source.Width, source.Height), StretchDirection.Both);
11784
}
11885

11986
return result;
@@ -122,17 +89,13 @@ protected override Size MeasureOverride(Size availableSize)
12289
/// <inheritdoc/>
12390
protected override Size ArrangeOverride(Size finalSize)
12491
{
125-
var source = Source?.Render();
126-
127-
if (source != null)
92+
if (Source?.Render() is SKBitmap source)
12893
{
12994
var sourceSize = new Size(source.Width, source.Height);
130-
var result = Stretch.CalculateSize(finalSize, sourceSize);
95+
var result = Stretch.Uniform.CalculateSize(finalSize, sourceSize);
13196
return result;
13297
}
133-
else
134-
{
135-
return new Size();
136-
}
98+
99+
return new Size();
137100
}
138101
}

Pixed.Application/Controls/PaintCanvas.axaml renamed to Pixed.Application/Controls/PaintCanvas/PaintControl.axaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<UserControl x:Class="Pixed.Application.Controls.PaintCanvas"
1+
<UserControl x:Class="Pixed.Application.Controls.PaintCanvas.PaintControl"
22
xmlns="https://github.com/avaloniaui"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:Pixed.Application.Controls"
7-
xmlns:controls="clr-namespace:Pixed.Application.Controls"
7+
xmlns:paintCanvas="clr-namespace:Pixed.Application.Controls.PaintCanvas"
88
xmlns:gestures="clr-namespace:Pixed.Application.Controls.Gestures"
99
xmlns:viewmodels="clr-namespace:Pixed.Application.ViewModels"
1010
xmlns:zoom="clr-namespace:Pixed.Application.Zoom"
1111
mc:Ignorable="d"
1212
d:DesignHeight="450" d:DesignWidth="800"
13-
x:DataType="viewmodels:PaintCanvasViewModel">
13+
x:DataType="viewmodels:PaintControlViewModel">
1414
<Grid>
1515
<Grid.RowDefinitions>
1616
<RowDefinition Height="*"/>
@@ -24,10 +24,10 @@
2424
<Border BorderBrush="{StaticResource ButtonBorder}" BorderThickness="2 0 2 2">
2525
<zoom:ZoomControl Name="zoomControl" GesturesEnabled="{Binding GestureZoomEnabled, Mode=TwoWay}" ZoomWidth="{Binding GridWidth, Mode=TwoWay}" ZoomHeight="{Binding GridHeight, Mode=TwoWay}">
2626
<zoom:ZoomControl.ZoomContent>
27-
<controls:TransparentBackground IsHitTestVisible="False" Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="transparentBackground"/>
28-
<controls:PaintCanvasImageControl Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
29-
<controls:ImageGrid Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
30-
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
27+
<paintCanvas:TransparentBackground IsHitTestVisible="False" Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="transparentBackground"/>
28+
<paintCanvas:PaintContainer Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
29+
<paintCanvas:GridOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
30+
<paintCanvas:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
3131
</zoom:ZoomControl.ZoomContent>
3232
</zoom:ZoomControl>
3333
</Border>

Pixed.Application/Controls/PaintCanvas.axaml.cs renamed to Pixed.Application/Controls/PaintCanvas/PaintControl.axaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
using Pixed.Core.Models;
1010
using System;
1111

12-
namespace Pixed.Application.Controls;
12+
namespace Pixed.Application.Controls.PaintCanvas;
1313

14-
internal partial class PaintCanvas : ExtendedControl<PaintCanvasViewModel>
14+
internal partial class PaintControl : ExtendedControl<PaintControlViewModel>
1515
{
1616
private readonly int _scrollBarSize = 18;
1717
private readonly IDisposable _zoomChanged;
18-
public PaintCanvas() : base()
18+
public PaintControl() : base()
1919
{
2020
InitializeComponent();
2121
SizeChanged += PaintCanvas_SizeChanged;

Pixed.Application/Controls/SelectionOverlay.cs renamed to Pixed.Application/Controls/PaintCanvas/SelectionOverlay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System;
88
using System.Collections.Generic;
99

10-
namespace Pixed.Application.Controls;
10+
namespace Pixed.Application.Controls.PaintCanvas;
1111
internal class SelectionOverlay : OverlayControl
1212
{
1313
private List<Tuple<SKPoint, SKPoint>> _lines = [];

Pixed.Application/Controls/TransparentBackground.cs renamed to Pixed.Application/Controls/PaintCanvas/TransparentBackground.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
using Pixed.Core;
44
using SkiaSharp;
55

6-
namespace Pixed.Application.Controls;
6+
namespace Pixed.Application.Controls.PaintCanvas;
77
internal class TransparentBackground : OverlayControl
88
{
99
private readonly SKBitmap _transparentBackground;
1010
private readonly SKShader _shader;
1111
private readonly SKPaint _paint;
1212
public TransparentBackground()
1313
{
14-
ClipToBounds = true;
14+
ClipToBounds = false;
1515
_transparentBackground = CreateTransparentBackground();
1616
_shader = SKShader.CreateBitmap(_transparentBackground, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat);
1717
_paint = new SKPaint()

Pixed.Application/Controls/PixelImageControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public sealed override void Render(DrawingContext context)
106106
context.DrawImage(_image, sourceRect, destRect);
107107
}
108108

109-
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Input);
109+
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
110110
}
111111

112112
/// <summary>

Pixed.Application/DependencyInjection/ApplicationDependencyRegister.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Register(ref IServiceCollection collection)
1919
collection.AddSingleton<FramesSectionViewModel>();
2020
collection.AddSingleton<LayersSectionViewModel>();
2121
collection.AddSingleton<MainViewModel>();
22-
collection.AddSingleton<PaintCanvasViewModel>();
22+
collection.AddSingleton<PaintControlViewModel>();
2323
collection.AddSingleton<PaletteSectionViewModel>();
2424
collection.AddSingleton<ProjectsSectionViewModel>();
2525
collection.AddSingleton<PropertiesSectionViewModel>();

Pixed.Application/Pages/Main.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:Pixed"
7-
xmlns:controls="clr-namespace:Pixed.Application.Controls"
7+
xmlns:paintCanvas="clr-namespace:Pixed.Application.Controls.PaintCanvas"
88
xmlns:mainsections="clr-namespace:Pixed.Application.Controls.MainWindowSections"
99
xmlns:dd="urn:gong-wpf-dragdrop"
1010
xmlns:viewmodels="clr-namespace:Pixed.Application.ViewModels"
@@ -35,7 +35,7 @@
3535
<RowDefinition Height="*"/>
3636
</Grid.RowDefinitions>
3737
<mainsections:ProjectsSection/>
38-
<controls:PaintCanvas Grid.Row="1" x:Name="paintCanvas"/>
38+
<paintCanvas:PaintControl Grid.Row="1" x:Name="paintCanvas"/>
3939
</Grid>
4040
<mainsections:PropertiesSection Grid.Column="2" Width="242"/>
4141
</Grid>

0 commit comments

Comments
 (0)