1- using Hi3Helper . Win32 . Native . Enums . DXGI ;
2- using Hi3Helper . Win32 . Native . Structs . DXGI ;
3- using Hi3Helper . Win32 . WinRT . SwapChainPanelHelper ;
4- using Microsoft . Graphics . Canvas ;
1+ using Microsoft . Graphics . Canvas ;
52using Microsoft . Graphics . Canvas . UI . Xaml ;
63using Microsoft . UI . Composition ;
74using Microsoft . UI . Xaml ;
85using Microsoft . UI . Xaml . Controls ;
96using Microsoft . UI . Xaml . Hosting ;
10- using System ;
117using System . Threading ;
128using Windows . Foundation ;
13- using Windows . Graphics . Capture ;
149using Windows . Media . Playback ;
15- using WinRT . Interop ;
1610
1711namespace BackgroundTest . CustomControl . LayeredBackgroundImage ;
18-
19- [ TemplatePart ( Name = TemplateNameParallaxGrid , Type = typeof ( Grid ) ) ]
20- [ TemplatePart ( Name = TemplateNamePlaceholderGrid , Type = typeof ( Grid ) ) ]
21- [ TemplatePart ( Name = TemplateNameBackgroundGrid , Type = typeof ( Grid ) ) ]
22- [ TemplatePart ( Name = TemplateNameForegroundGrid , Type = typeof ( Grid ) ) ]
2312public partial class LayeredBackgroundImage
2413{
2514 #region Constants
@@ -35,10 +24,10 @@ public partial class LayeredBackgroundImage
3524
3625 #region Fields
3726
38- private Grid _parallaxGrid = null ! ;
27+ private Grid _parallaxGrid = null ! ;
3928 private Grid _placeholderGrid = null ! ;
40- private Grid _backgroundGrid = null ! ;
41- private Grid _foregroundGrid = null ! ;
29+ private Grid _backgroundGrid = null ! ;
30+ private Grid _foregroundGrid = null ! ;
4231
4332 private Visual _parallaxGridVisual = null ! ;
4433 private Compositor _parallaxGridCompositor = null ! ;
@@ -50,44 +39,23 @@ public partial class LayeredBackgroundImage
5039 private CanvasDrawingSession ? _currentCanvasDrawingSession = null ;
5140 private Rect _currentCanvasRenderSize = default ;
5241
53- private SwapChainContext _swapChainContext = null ! ;
54- private SwapChainPanel _swapChainPanel = null ! ;
55-
5642 private float _canvasWidth ;
5743 private float _canvasHeight ;
58- private float _lastCanvasWidth ;
59- private float _lastCanvasHeight ;
6044
6145 private MediaPlayer _videoPlayer = null ! ;
6246
6347 private bool _isTemplateLoaded ;
6448
6549 #endregion
6650
67- #region Deconstructor
68-
69- ~ LayeredBackgroundImage ( )
70- {
71- if ( _videoPlayer == null ! )
72- {
73- return ;
74- }
75-
76- _videoPlayer . Dispose ( ) ;
77- _videoPlayer = null ! ;
78- }
79- #endregion
80-
8151 #region Apply Template Methods
8252
8353 protected override void OnApplyTemplate ( )
8454 {
85- base . OnApplyTemplate ( ) ;
86-
87- _parallaxGrid = this . GetTemplateChild < Grid > ( TemplateNameParallaxGrid ) ;
55+ _parallaxGrid = this . GetTemplateChild < Grid > ( TemplateNameParallaxGrid ) ;
8856 _placeholderGrid = this . GetTemplateChild < Grid > ( TemplateNamePlaceholderGrid ) ;
89- _backgroundGrid = this . GetTemplateChild < Grid > ( TemplateNameBackgroundGrid ) ;
90- _foregroundGrid = this . GetTemplateChild < Grid > ( TemplateNameForegroundGrid ) ;
57+ _backgroundGrid = this . GetTemplateChild < Grid > ( TemplateNameBackgroundGrid ) ;
58+ _foregroundGrid = this . GetTemplateChild < Grid > ( TemplateNameForegroundGrid ) ;
9159
9260 ElementCompositionPreview . SetIsTranslationEnabled ( _parallaxGrid , true ) ;
9361
@@ -96,12 +64,11 @@ protected override void OnApplyTemplate()
9664
9765 Interlocked . Exchange ( ref _isTemplateLoaded , true ) ;
9866
99- Loaded += LayeredBackgroundImage_OnLoaded ;
100- Unloaded += LayeredBackgroundImage_OnUnloaded ;
101-
10267#if USENATIVESWAPCHAIN
10368 SizeChanged += LayeredBackgroundImage_SizeChanged ;
10469#endif
70+
71+ base . OnApplyTemplate ( ) ;
10572 }
10673
10774 private void SetupVideoPlayer ( )
@@ -118,40 +85,9 @@ private void SetupVideoPlayer()
11885 _videoPlayer . VideoFrameAvailable += VideoPlayer_VideoFrameAvailable ;
11986 }
12087
121- private void DisposeVideoPlayer ( )
122- {
123- MediaPlayer ? pastMediaPlayer = _videoPlayer ;
124- if ( pastMediaPlayer != null ! )
125- {
126- pastMediaPlayer . VideoFrameAvailable -= VideoPlayer_VideoFrameAvailable ;
127- pastMediaPlayer . Dispose ( ) ;
128- }
129-
130- if ( _currentCanvasDrawingSession is { } previousCanvasDrawingSession )
131- {
132- previousCanvasDrawingSession . Dispose ( ) ;
133- Interlocked . Exchange ( ref _currentCanvasDrawingSession , null ) ;
134- }
135-
136- Interlocked . Exchange ( ref _isDrawingVideoFrame , false ) ;
137- }
138-
13988 private void InitializeCanvasBitmapSource ( Image image , MediaPlaybackSession playbackSession )
14089 {
141- if ( Interlocked . Exchange ( ref _isResizingVideoCanvas , true ) )
142- {
143- return ;
144- }
145-
146- if ( _canvasRenderTarget is { } previousCanvasBitmap )
147- {
148- previousCanvasBitmap . Dispose ( ) ;
149- }
150-
151- if ( _canvasDevice is { } previousCanvasDevice )
152- {
153- previousCanvasDevice . Dispose ( ) ;
154- }
90+ DisposeAndInvalidateCanvas ( ) ;
15591
15692 float currentCanvasWidth = playbackSession . NaturalVideoWidth ;
15793 float currentCanvasHeight = playbackSession . NaturalVideoHeight ;
@@ -168,7 +104,7 @@ private void InitializeCanvasBitmapSource(Image image, MediaPlaybackSession play
168104 currentCanvasHeight ,
169105 currentCanvasDpi ,
170106 CanvasAlphaMode . Premultiplied ) ;
171- _canvasRenderTarget = new CanvasRenderTarget ( _canvasImageSource ,
107+ _canvasRenderTarget = new CanvasRenderTarget ( _canvasDevice ,
172108 currentCanvasWidth ,
173109 currentCanvasHeight ,
174110 currentCanvasDpi ) ;
@@ -182,6 +118,42 @@ private void InitializeCanvasBitmapSource(Image image, MediaPlaybackSession play
182118 Interlocked . Exchange ( ref _isResizingVideoCanvas , false ) ;
183119 }
184120
121+ private void DisposeAndInvalidateCanvas ( )
122+ {
123+ if ( Interlocked . Exchange ( ref _isResizingVideoCanvas , true ) )
124+ {
125+ return ;
126+ }
127+
128+ if ( _canvasRenderTarget is { } previousCanvasBitmap )
129+ {
130+ previousCanvasBitmap . Dispose ( ) ;
131+ }
132+
133+ if ( _canvasDevice is { } previousCanvasDevice )
134+ {
135+ previousCanvasDevice . Dispose ( ) ;
136+ }
137+ }
138+
139+ private void DisposeVideoPlayer ( )
140+ {
141+ if ( _videoPlayer != null ! )
142+ {
143+ _videoPlayer . VideoFrameAvailable -= VideoPlayer_VideoFrameAvailable ;
144+ _videoPlayer . Dispose ( ) ;
145+ Interlocked . Exchange ( ref _videoPlayer ! , null ) ;
146+ }
147+
148+ if ( _currentCanvasDrawingSession is { } previousCanvasDrawingSession )
149+ {
150+ previousCanvasDrawingSession . Dispose ( ) ;
151+ Interlocked . Exchange ( ref _currentCanvasDrawingSession , null ) ;
152+ }
153+
154+ Interlocked . Exchange ( ref _isDrawingVideoFrame , false ) ;
155+ }
156+
185157#if USENATIVESWAPCHAIN
186158 private void LayeredBackgroundImage_SizeChanged ( object sender , SizeChangedEventArgs e )
187159 {
@@ -241,6 +213,6 @@ private void InitializeOrResizeCanvas(bool ignoreCheck = false)
241213 }
242214#endif
243215
244- #endregion
216+ #endregion
245217}
246218
0 commit comments