From 5be8b8587faf7c1ab22810022a38bf932838d9dc Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Sun, 8 Nov 2026 21:07:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=B8=A7=E5=BF=AB=E7=85=A7=E5=88=B7=E6=96=B0?= =?UTF-8?q?=20Avalonia=20=E7=9B=B8=E6=9C=BA=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VVMs/SopPage/SopPageViewModel.cs | 13 ++++++- tests/Avalonia.Smoke/Program.cs | 36 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs index 8e5cb42..af6c6d0 100644 --- a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs @@ -360,6 +360,12 @@ public class SopPageViewModel : ViewModelBase _pending = null; } + var previous = Frame as BitmapSource; + Frame = null; + previous?.Dispose(); + _bitmap?.Dispose(); + _bitmap = null; + // 相机与推理管线是应用级单例,由 App.OnExit 按顺序统一释放。 base.Destroy(); @@ -684,6 +690,7 @@ public class SopPageViewModel : ViewModelBase { if (_bitmap is null || _bitmap.PixelWidth != src.Width || _bitmap.PixelHeight != src.Height) { + _bitmap?.Dispose(); _bitmap = new WriteableBitmap(src.Cols, src.Rows, 96, 96, PixelFormats.Bgr24, null); // ROI 覆盖层的坐标系就是帧像素坐标系,所以内层 Grid 尺寸必须跟着帧走 @@ -702,7 +709,11 @@ public class SopPageViewModel : ViewModelBase _bitmap.WritePixels(new Avalonia.PixelRect(0, 0, src.Cols, src.Rows), bytes, stride, 0); } - Frame = _bitmap; + // 同一 IImage 引用不会触发 Avalonia Image 的重绘;覆盖层的局部刷新会留下旧画面。 + // 发布独立快照,同时避免渲染线程读取正在写入的位图。 + var previous = Frame as BitmapSource; + Frame = _bitmap.Snapshot(); + previous?.Dispose(); // 帧率:这 40ms 里处理了几帧 var delta = _frameCount - _lastTickCount; diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index 5aae521..2ab21ff 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -70,6 +70,7 @@ internal static class Smoke await Task.Run(()=>{Camera.Emit();Camera.Emit();}); await Task.Delay(150); Check(sop.Frame is not null&&sop.FrameWidth==640&&sop.FrameHeight==480,"Camera buffer renders through the shared Avalonia bitmap adapter"); + await CheckMovingPreview(sop,window); Save(window,"sop"); sop.StartStopCommand.Execute();for(var i=0;i<100&&sop.IsRunning;i++)await Task.Delay(20); Check(!sop.IsRunning&&!Camera.IsConnected,"Stop command releases acquisition"); @@ -140,6 +141,38 @@ internal static class Smoke finally{lifetime.Shutdown(_exit);} } private static string Output {get{var p=Path.GetFullPath("artifacts/avalonia-smoke");Directory.CreateDirectory(p);return p;}} + private static async Task CheckMovingPreview(SopPageViewModel sop,Window window) + { + var image=window.GetVisualDescendants().OfType().Single() + .GetVisualDescendants().OfType().Single(); + var changes=0; + System.ComponentModel.PropertyChangedEventHandler onChanged=(_,e)=>{if(e.PropertyName==nameof(sop.Frame))changes++;}; + sop.PropertyChanged+=onChanged; + try + { + foreach(var x in new[]{40,240,40}) + { + var previous=sop.Frame; + await Task.Run(()=>Camera.Emit(x)); + for(var i=0;i<50&&ReferenceEquals(previous,sop.Frame);i++)await Task.Delay(20); + Check(!ReferenceEquals(previous,sop.Frame)&&ReferenceEquals(image.Source,sop.Frame), + "Each moving frame updates the bound Image source even at the same resolution"); + using var rendered=new RenderTargetBitmap(new PixelSize(640,480)); + var preview=new Image {Source=sop.Frame,Width=640,Height=480}; + preview.Measure(new Size(640,480));preview.Arrange(new Avalonia.Rect(0,0,640,480)); + rendered.Render(preview); + var path=Path.Combine(Output,"motion-"+changes+".png");rendered.Save(path); + using var decoded=Cv2.ImRead(path); + var clearedX=x==40?260:60; + Check(decoded.At(60,x+20)==new Vec3b(0,0,255)&&decoded.At(60,clearedX)==new Vec3b(35,60,90), + "Moving object renders at its new position and clears its previous position"); + } + Check(changes==3,"All three same-size frames notify the UI"); + var last=sop.Frame;await Task.Delay(120); + Check(ReferenceEquals(last,sop.Frame),"No new camera frame leaves the displayed image intact"); + } + finally{sop.PropertyChanged-=onChanged;} + } private static void Save(Window window,string name) { window.UpdateLayout(); @@ -158,9 +191,10 @@ internal sealed class FakeCamera : ICameraService public Task StartAsync(){IsGrabbing=true;return Task.FromResult(true);} public void Stop(){IsGrabbing=false;}public void Disconnect(){Stop();IsConnected=false;ConnectionChanged?.Invoke(false);}public void Dispose()=>Disconnect(); private long _id; - public void Emit() + public void Emit(int? objectX=null) { using var frame=new CameraFrame {FrameId=++_id,Timestamp=DateTime.Now,Bgr=new Mat(480,640,MatType.CV_8UC3,new Scalar(35,60,90))}; + if(objectX is int x)Cv2.Rectangle(frame.Bgr,new OpenCvSharp.Rect(x,40,60,60),new Scalar(0,0,255),-1); FrameArrived?.Invoke(frame); } }