Skip to content

Commit 46656ea

Browse files
committed
Use unsafe direct call for video frame drawing's EndDraw()
1 parent 5a815b9 commit 46656ea

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.Events.FrameRenderer.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Hi3Helper.Win32.WinRT.SwapChainPanelHelper;
1+
using Hi3Helper.Win32.ManagedTools;
2+
using Hi3Helper.Win32.Native.Interfaces.DXGI;
3+
using Hi3Helper.Win32.WinRT.SwapChainPanelHelper;
24
using Microsoft.Graphics.Canvas;
35
using Microsoft.UI.Xaml;
46
using Microsoft.UI.Xaml.Controls;
@@ -65,7 +67,8 @@ private unsafe void VideoPlayer_VideoFrameAvailable(MediaPlayer sender, object a
6567
out nint surfacePpv);
6668

6769
SwapChainPanelHelper.MediaPlayer_CopyFrameToVideoSurfaceUnsafe(_videoPlayerPtr, surfacePpv);
68-
_canvasSurfaceImageSourceNative.EndDraw();
70+
// _canvasSurfaceImageSourceNative.EndDraw();
71+
SwapChainPanelHelper.EndDrawNativeSurfaceImageSource(_canvasSurfaceImageSourceNativePtr);
6972
Marshal.Release(surfacePpv);
7073
});
7174
// sender.CopyFrameToVideoSurface(_canvasRenderTarget);
@@ -137,7 +140,15 @@ private void InitializeRenderTarget()
137140

138141
_canvasDevice = CanvasDevice.GetSharedDevice();
139142
_canvasSurfaceImageSource = new(_canvasWidth, _canvasHeight, true);
140-
SwapChainPanelHelper.GetNativeSurfaceImageSource(_canvasSurfaceImageSource, out _canvasSurfaceImageSourceNative);
143+
SwapChainPanelHelper.GetNativeSurfaceImageSource(_canvasSurfaceImageSource,
144+
out _canvasSurfaceImageSourceNative,
145+
out _canvasD3DDeviceContext);
146+
147+
ComMarshal<ISurfaceImageSourceNativeWithD2D>.TryGetComInterfaceReference(_canvasSurfaceImageSourceNative,
148+
out _canvasSurfaceImageSourceNativePtr,
149+
out _,
150+
CreateComInterfaceFlags.None,
151+
true);
141152

142153
if (FindRenderImage() is Image image)
143154
{
@@ -147,13 +158,23 @@ private void InitializeRenderTarget()
147158

148159
private void DisposeRenderTarget()
149160
{
161+
// Dispose D3DDeviceContext and its dependencies
162+
_canvasD3DDeviceContext?.Dispose();
163+
_canvasSurfaceImageSourceNative?.SetDevice(nint.Zero);
164+
ComMarshal<ISurfaceImageSourceNativeWithD2D>.TryReleaseComObject(_canvasSurfaceImageSourceNative, out _);
165+
150166
_canvasDevice?.Dispose();
151167
if (FindRenderImage() is Image image)
152168
{
153169
image.Source = null;
154170
}
155-
171+
172+
_canvasSurfaceImageSourceNativePtr = nint.Zero;
173+
156174
Interlocked.Exchange(ref _canvasDevice, null);
175+
Interlocked.Exchange(ref _canvasD3DDeviceContext, null);
176+
Interlocked.Exchange(ref _canvasSurfaceImageSourceNative, null);
177+
Interlocked.Exchange(ref _canvasSurfaceImageSource, null);
157178
}
158179

159180
private Image? FindRenderImage() => _backgroundGrid.Children
@@ -217,6 +238,11 @@ public void Play()
217238
{
218239
Console.WriteLine(e);
219240
}
241+
finally
242+
{
243+
GC.Collect();
244+
GC.WaitForPendingFinalizers();
245+
}
220246
}
221247

222248
public void Pause()
@@ -239,6 +265,11 @@ public void Pause()
239265
{
240266
Console.WriteLine(e);
241267
}
268+
finally
269+
{
270+
GC.Collect();
271+
GC.WaitForPendingFinalizers();
272+
}
242273
}
243274

244275
#endregion

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.Templates.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Hi3Helper.Win32.Native.Interfaces.DXGI;
2+
using Hi3Helper.Win32.WinRT.SwapChainPanelHelper;
23
using Microsoft.Graphics.Canvas;
34
using Microsoft.UI.Composition;
45
using Microsoft.UI.Xaml.Controls;
@@ -33,10 +34,11 @@ public partial class LayeredBackgroundImage
3334
private Visual _parallaxGridVisual = null!;
3435
private Compositor _parallaxGridCompositor = null!;
3536

37+
private D3DDeviceContext? _canvasD3DDeviceContext;
3638
private ISurfaceImageSourceNativeWithD2D? _canvasSurfaceImageSourceNative;
39+
private nint _canvasSurfaceImageSourceNativePtr = nint.Zero;
3740
private SurfaceImageSource? _canvasSurfaceImageSource;
3841

39-
4042
private CanvasDevice? _canvasDevice = null!;
4143
private int _canvasWidth;
4244
private int _canvasHeight;

0 commit comments

Comments
 (0)