Skip to content

Commit 7751c57

Browse files
committed
Lock drawing routine and allow frame skip
+ This allows other frames to be skipped if the first frame doesn't have enough time to be drawn between other frames (in some cases, devices with lower specs).
1 parent 35eede7 commit 7751c57

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

CollapseLauncher/Classes/Helper/Background/Loaders/MediaPlayerLoader.cs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using static Hi3Helper.Logger;
3232
// ReSharper disable PartialTypeWithSinglePart
3333
// ReSharper disable StringLiteralTypo
34+
// ReSharper disable AsyncVoidMethod
3435

3536
#nullable enable
3637
namespace CollapseLauncher.Helper.Background.Loaders
@@ -71,6 +72,7 @@ private static bool IsUseVideoBgDynamicColorUpdate
7172
private readonly MediaPlayerElement? _currentMediaPlayerFrame;
7273
private readonly Grid _currentMediaPlayerFrameParentGrid;
7374
private readonly ImageUI _currentImage;
75+
private readonly Lock _frameGrabberEventLock = new();
7476

7577
internal MediaPlayerLoader(
7678
FrameworkElement parentUI,
@@ -325,37 +327,42 @@ await ColorPaletteUtility.ApplyAccentColor(ParentUI,
325327
private static async Task<StorageFile> GetFileAsStorageFile(string filePath)
326328
=> await StorageFile.GetFileFromPathAsync(filePath);
327329

328-
private void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
330+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
331+
private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
332+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
329333
{
330334
if (_isCanvasCurrentlyDrawing == 1)
331335
{
332336
return;
333337
}
334338

335-
CanvasDrawingSession? drawingSession = null;
336-
try
337-
{
338-
Interlocked.Exchange(ref _isCanvasCurrentlyDrawing, 1);
339-
mediaPlayer.CopyFrameToVideoSurface(_currentCanvasBitmap);
340-
drawingSession = _currentCanvasVirtualImageSource?.CreateDrawingSession(_currentDefaultColor, _currentCanvasDrawArea);
341-
drawingSession?.DrawImage(_currentCanvasBitmap);
342-
_currentCanvasVirtualImageSource?.SuspendDrawingSession(drawingSession);
343-
}
344-
catch
345-
#if DEBUG
346-
(Exception e)
347-
{
348-
LogWriteLine($"[FrameGrabberEvent] Error while drawing frame to bitmap.\r\n{e}", LogType.Warning, true);
349-
}
350-
#else
351-
{
352-
// ignored
353-
}
354-
#endif
355-
finally
339+
lock (_frameGrabberEventLock)
356340
{
357-
CurrentDispatcherQueue.TryEnqueue(() => drawingSession?.Dispose());
358-
Interlocked.Exchange(ref _isCanvasCurrentlyDrawing, 0);
341+
_isCanvasCurrentlyDrawing = 1;
342+
CanvasDrawingSession? drawingSession = null;
343+
344+
try
345+
{
346+
mediaPlayer.CopyFrameToVideoSurface(_currentCanvasBitmap);
347+
drawingSession = _currentCanvasVirtualImageSource?.CreateDrawingSession(_currentDefaultColor, _currentCanvasDrawArea);
348+
drawingSession?.DrawImage(_currentCanvasBitmap);
349+
}
350+
catch
351+
#if DEBUG
352+
(Exception e)
353+
{
354+
LogWriteLine($"[FrameGrabberEvent] Error while drawing frame to bitmap.\r\n{e}", LogType.Warning, true);
355+
}
356+
#else
357+
{
358+
// ignored
359+
}
360+
#endif
361+
finally
362+
{
363+
CurrentDispatcherQueue.TryEnqueue(() => drawingSession?.Dispose());
364+
_isCanvasCurrentlyDrawing = 0;
365+
}
359366
}
360367
}
361368

0 commit comments

Comments
 (0)