feat: WinRT Ink Analysis 性能优化重构方案
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
This commit is contained in:
@@ -17,48 +17,16 @@ namespace Ink_Canvas.Helpers
|
|||||||
public static readonly Guid ShapeStrokePropertyGuid = new Guid("11111111-2222-3333-4444-555555555555");
|
public static readonly Guid ShapeStrokePropertyGuid = new Guid("11111111-2222-3333-4444-555555555555");
|
||||||
|
|
||||||
private global::Windows.UI.Input.Inking.Analysis.InkAnalyzer _internalAnalyzer;
|
private global::Windows.UI.Input.Inking.Analysis.InkAnalyzer _internalAnalyzer;
|
||||||
private readonly StrokeCollection _strokeContainer;
|
|
||||||
private readonly Dictionary<Stroke, uint> _strokeIdMap = new Dictionary<Stroke, uint>();
|
private readonly Dictionary<Stroke, uint> _strokeIdMap = new Dictionary<Stroke, uint>();
|
||||||
private readonly Dictionary<uint, Stroke> _reverseIdMap = new Dictionary<uint, Stroke>();
|
private readonly Dictionary<uint, Stroke> _reverseIdMap = new Dictionary<uint, Stroke>();
|
||||||
private readonly object _syncLock = new object();
|
private readonly object _syncLock = new object();
|
||||||
|
|
||||||
public ModernInkAnalyzer(StrokeCollection container)
|
public ModernInkAnalyzer()
|
||||||
{
|
{
|
||||||
if (!WinRtInkShapeRecognizer.IsApiAvailable)
|
if (!WinRtInkShapeRecognizer.IsApiAvailable)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_internalAnalyzer = new global::Windows.UI.Input.Inking.Analysis.InkAnalyzer();
|
_internalAnalyzer = new global::Windows.UI.Input.Inking.Analysis.InkAnalyzer();
|
||||||
_strokeContainer = container;
|
|
||||||
_strokeContainer.StrokesChanged += OnStrokesChanged;
|
|
||||||
|
|
||||||
// Initial sync
|
|
||||||
foreach (var stroke in _strokeContainer)
|
|
||||||
{
|
|
||||||
AddStrokeInternal(stroke);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnStrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (_internalAnalyzer == null) return;
|
|
||||||
|
|
||||||
lock (_syncLock)
|
|
||||||
{
|
|
||||||
foreach (var stroke in e.Added)
|
|
||||||
{
|
|
||||||
AddStrokeInternal(stroke);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var stroke in e.Removed)
|
|
||||||
{
|
|
||||||
if (_strokeIdMap.TryGetValue(stroke, out var id))
|
|
||||||
{
|
|
||||||
_internalAnalyzer.RemoveDataForStroke(id);
|
|
||||||
_strokeIdMap.Remove(stroke);
|
|
||||||
_reverseIdMap.Remove(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddStrokeInternal(Stroke stroke)
|
private void AddStrokeInternal(Stroke stroke)
|
||||||
@@ -80,9 +48,9 @@ namespace Ink_Canvas.Helpers
|
|||||||
|
|
||||||
private CancellationTokenSource _cts;
|
private CancellationTokenSource _cts;
|
||||||
|
|
||||||
public async Task<InkShapeRecognitionResult> AnalyzeAsync()
|
public async Task<InkShapeRecognitionResult> AnalyzeAsync(StrokeCollection strokes)
|
||||||
{
|
{
|
||||||
if (_internalAnalyzer == null)
|
if (_internalAnalyzer == null || strokes == null || strokes.Count == 0)
|
||||||
return InkShapeRecognitionResult.Empty;
|
return InkShapeRecognitionResult.Empty;
|
||||||
|
|
||||||
_cts?.Cancel();
|
_cts?.Cancel();
|
||||||
@@ -91,6 +59,21 @@ namespace Ink_Canvas.Helpers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
lock (_syncLock)
|
||||||
|
{
|
||||||
|
_internalAnalyzer.ClearDataForAllStrokes();
|
||||||
|
_strokeIdMap.Clear();
|
||||||
|
_reverseIdMap.Clear();
|
||||||
|
|
||||||
|
foreach (var stroke in strokes)
|
||||||
|
{
|
||||||
|
AddStrokeInternal(stroke);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_strokeIdMap.Count == 0)
|
||||||
|
return InkShapeRecognitionResult.Empty;
|
||||||
|
|
||||||
var result = await _internalAnalyzer.AnalyzeAsync().AsTask(token).ConfigureAwait(true);
|
var result = await _internalAnalyzer.AnalyzeAsync().AsTask(token).ConfigureAwait(true);
|
||||||
|
|
||||||
if (token.IsCancellationRequested) return InkShapeRecognitionResult.Empty;
|
if (token.IsCancellationRequested) return InkShapeRecognitionResult.Empty;
|
||||||
@@ -147,10 +130,6 @@ namespace Ink_Canvas.Helpers
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_strokeContainer != null)
|
|
||||||
{
|
|
||||||
_strokeContainer.StrokesChanged -= OnStrokesChanged;
|
|
||||||
}
|
|
||||||
_internalAnalyzer = null;
|
_internalAnalyzer = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ namespace Ink_Canvas
|
|||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
private Helpers.ModernInkAnalyzer _modernInkAnalyzer;
|
private Helpers.ModernInkAnalyzer _modernInkAnalyzer;
|
||||||
private Helpers.ModernInkAnalyzer ModernInkAnalyzer =>
|
private Helpers.ModernInkAnalyzer ModernInkAnalyzer =>
|
||||||
_modernInkAnalyzer ??= new Helpers.ModernInkAnalyzer(inkCanvas.Strokes);
|
_modernInkAnalyzer ??= new Helpers.ModernInkAnalyzer();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 存储新的笔画集合,用于形状识别
|
/// 存储新的笔画集合,用于形状识别
|
||||||
@@ -626,7 +626,7 @@ namespace Ink_Canvas
|
|||||||
|
|
||||||
if (ShapeRecognitionRouter.ResolveUseWinRt(shapeMode) && Helpers.WinRtInkShapeRecognizer.IsApiAvailable)
|
if (ShapeRecognitionRouter.ResolveUseWinRt(shapeMode) && Helpers.WinRtInkShapeRecognizer.IsApiAvailable)
|
||||||
{
|
{
|
||||||
result = await ModernInkAnalyzer.AnalyzeAsync();
|
result = await ModernInkAnalyzer.AnalyzeAsync(newStrokes);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user