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

Android - Defer rendering till surface is created #16616

Merged
merged 1 commit into from
Aug 13, 2024
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
2 changes: 1 addition & 1 deletion src/Android/Avalonia.Android/AvaloniaActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected override void OnDestroy()

base.OnDestroy();
}

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent? data)
{
base.OnActivityResult(requestCode, resultCode, data);
Expand Down
2 changes: 1 addition & 1 deletion src/Android/Avalonia.Android/AvaloniaMainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ private protected override void InitializeAvaloniaView(object? initialContent)
{
initialContent ??= Lifetime.MainView;

Lifetime.Activity = this;
_view = new AvaloniaView(this) { Content = initialContent };
Lifetime.Activity = this;
}
else
{
Expand Down
20 changes: 18 additions & 2 deletions src/Android/Avalonia.Android/AvaloniaView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class AvaloniaView : FrameLayout
private readonly ViewImpl _view;

private IDisposable? _timerSubscription;
private bool _surfaceCreated;

public AvaloniaView(Context context) : base(context)
{
Expand All @@ -32,6 +33,18 @@ public AvaloniaView(Context context) : base(context)

this.SetBackgroundColor(global::Android.Graphics.Color.Transparent);
OnConfigurationChanged();

_view.InternalView.SurfaceWindowCreated += InternalView_SurfaceWindowCreated;
}

private void InternalView_SurfaceWindowCreated(object? sender, EventArgs e)
{
_surfaceCreated = true;

if (Visibility == ViewStates.Visible)
{
OnVisibilityChanged(true);
}
}

internal TopLevelImpl TopLevelImpl => _view;
Expand All @@ -43,9 +56,10 @@ public object? Content
set { _root.Content = value; }
}

protected override void Dispose(bool disposing)
internal new void Dispose()
{
base.Dispose(disposing);
OnVisibilityChanged(false);
_surfaceCreated = false;
_root?.Dispose();
_root = null!;
}
Expand All @@ -70,6 +84,8 @@ protected override void OnVisibilityChanged(View changedView, [GeneratedEnum] Vi

internal void OnVisibilityChanged(bool isVisible)
{
if (_root == null || !_surfaceCreated)
return;
if (isVisible && _timerSubscription == null)
{
if (AvaloniaLocator.Current.GetService<IRenderTimer>() is ChoreographerTimer timer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ namespace Avalonia.Android
internal abstract class InvalidationAwareSurfaceView : SurfaceView, ISurfaceHolderCallback, INativePlatformHandleSurface
{
bool _invalidateQueued;
private bool _isDisposed;
readonly object _lock = new object();
private readonly Handler _handler;

internal event EventHandler? SurfaceWindowCreated;

IntPtr IPlatformHandle.Handle => Holder?.Surface?.Handle is { } handle ?
AndroidFramebuffer.ANativeWindow_fromSurface(JNIEnv.Handle, handle) :
default;
Expand All @@ -39,7 +42,7 @@ public override void Invalidate()
return;
_handler.Post(() =>
{
if (Holder?.Surface?.IsValid != true)
if (_isDisposed || Holder?.Surface?.IsValid != true)
return;
try
{
Expand All @@ -53,6 +56,11 @@ public override void Invalidate()
}
}

internal new void Dispose()
{
_isDisposed = true;
}

public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
{
Log.Info("AVALONIA", "Surface Changed");
Expand All @@ -62,6 +70,7 @@ public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int
public void SurfaceCreated(ISurfaceHolder holder)
{
Log.Info("AVALONIA", "Surface Created");
SurfaceWindowCreated?.Invoke(this, EventArgs.Empty);
DoDraw();
}

Expand Down
Loading