Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be a bit more defensive in the display links #1620

Merged
merged 3 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CoreVideo;
using System;
using CoreVideo;
using Xamarin.Forms;

using SKFormsView = SkiaSharp.Views.Forms.SKGLView;
Expand Down Expand Up @@ -46,7 +47,7 @@ protected override void SetupRenderLoop(bool oneShot)
var nativeView = Control;
nativeView?.BeginInvokeOnMainThread(() =>
{
if (nativeView != null)
if (nativeView.Handle != IntPtr.Zero)
nativeView.NeedsDisplay = true;
});
return;
Expand All @@ -59,21 +60,22 @@ protected override void SetupRenderLoop(bool oneShot)
var nativeView = Control;
var formsView = Element;

// redraw the view
nativeView?.BeginInvokeOnMainThread(() =>
{
if (nativeView != null)
nativeView.NeedsDisplay = true;
});

// stop the render loop if this was a one-shot, or the views are disposed
if (nativeView == null || formsView == null || !formsView.HasRenderLoop)
if (nativeView == null || formsView == null || nativeView.Handle == IntPtr.Zero || !formsView.HasRenderLoop)
{
displayLink.Stop();
displayLink.Dispose();
displayLink = null;
return CVReturn.Success;
}

// redraw the view
nativeView?.BeginInvokeOnMainThread(() =>
{
if (nativeView != null)
nativeView.NeedsDisplay = true;
});

return CVReturn.Success;
});
displayLink.Start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CoreAnimation;
using System;
using CoreAnimation;
using Foundation;
using Xamarin.Forms;

Expand Down Expand Up @@ -39,7 +40,7 @@ protected override void Dispose(bool disposing)
displayLink.Dispose();
displayLink = null;
}

base.Dispose(disposing);
}

Expand All @@ -57,7 +58,11 @@ protected override void SetupRenderLoop(bool oneShot)
if (oneShot)
{
var nativeView = Control;
nativeView?.BeginInvokeOnMainThread(() => nativeView?.Display());
nativeView?.BeginInvokeOnMainThread(() =>
{
if (nativeView.Handle != IntPtr.Zero)
nativeView.Display();
});
return;
}

Expand All @@ -67,16 +72,17 @@ protected override void SetupRenderLoop(bool oneShot)
var nativeView = Control;
var formsView = Element;

// redraw the view
nativeView?.Display();

// stop the render loop if this was a one-shot, or the views are disposed
if (nativeView == null || formsView == null || !formsView.HasRenderLoop)
if (nativeView == null || formsView == null || nativeView.Handle == IntPtr.Zero || !formsView.HasRenderLoop)
{
displayLink.Invalidate();
displayLink.Dispose();
displayLink = null;
return;
}

// redraw the view
nativeView.Display();
});
displayLink.AddToRunLoop(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
}
Expand Down