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

remove Resuming state #1578

Merged
merged 1 commit into from
May 18, 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
55 changes: 17 additions & 38 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ public class AndroidSurfaceView : SurfaceView
, ISurfaceHolderCallback
, Java.Lang.IRunnable
{
// What is the state of the app, for tracking surface recreation inside this class.
// What is the state of the app.
enum AppState
{
Resuming, // set by android UI thread process it and transitions into 'Running' state

Paused, // set by game thread after processing 'Pausing' state
Running, // set by game thread after processing 'Resuming' state
Exited, // set by game thread after processing 'Exiting' state
Paused,
Resumed,
Exited,
}

ISurfaceHolder _surfaceHolder;
Expand Down Expand Up @@ -75,11 +73,11 @@ public AndroidSurfaceView(Context context, AndroidGameWindow gameWindow, Game ga

void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
{
// Set flag to recreate gl surface or rendering can be bad on orientation change or if app
// is closed in one orientation and re-opened in another.
// Set flag to recreate gl surface or rendering can be bad on orientation change
// or if app is closed in one orientation and re-opened in another.

// can only be triggered when main loop is running, is unsafe to overwrite other states
if (_appState == AppState.Running)
if (_appState == AppState.Resumed)
_isAndroidSurfaceChanged = true;
}

Expand Down Expand Up @@ -159,8 +157,7 @@ void Java.Lang.IRunnable.Run()
finally
{
// request next tick
if (_appState == AppState.Resuming
|| _appState == AppState.Running)
if (_appState == AppState.Resumed)
RequestFrame();
}
}
Expand Down Expand Up @@ -210,16 +207,11 @@ void RunStep()

switch (_appState)
{
case AppState.Resuming: // when ui thread wants to resume
processStateResuming();
break;

case AppState.Running: // when we are running game
processStateRunning();
case AppState.Resumed:
processStateResumed();
break;

case AppState.Paused: // when game thread processed pausing event
// this must be processed outside of this loop, in the new task thread!
case AppState.Paused:
break;

case AppState.Exited:
Expand All @@ -233,10 +225,9 @@ void RunStep()
return;
}

void processStateResuming()
void processStateResumed()
{
// this can happen if pause is triggered immediately after resume so that SurfaceCreated callback doesn't get called yet,
// in this case we skip the resume process and pause sets a new state.
// do not run game if surface is not available
if (_isAndroidSurfaceAvailable)
{
// create surface if context is available
Expand All @@ -245,6 +236,7 @@ void processStateResuming()
if (_eglSurface == null)
{
CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
Expand Down Expand Up @@ -289,6 +281,7 @@ void processStateResuming()
if (_eglSurface == null)
{
CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
Expand All @@ -306,18 +299,6 @@ void processStateResuming()
}
}

_isAndroidSurfaceChanged = false;

// go to next state
_appState = AppState.Running;
}
}

void processStateRunning()
{
// do not run game if surface is not available
if (_isAndroidSurfaceAvailable)
{
// needed at app start
if (_eglContext != null && _isAndroidSurfaceChanged)
{
Expand All @@ -330,10 +311,9 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}

// check if app wants to exit
Expand Down Expand Up @@ -363,7 +343,7 @@ void processStateRunning()

internal void Resume()
{
_appState = AppState.Resuming;
_appState = AppState.Resumed;
RequestFrame();

try
Expand All @@ -379,7 +359,6 @@ internal void Resume()

internal void Pause()
{
// prepare for next game loop iteration
_appState = AppState.Paused;
}

Expand Down
54 changes: 18 additions & 36 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ public class AndroidSurfaceView : VRCardboard.CardboardView
, ISurfaceView
, VRCardboard.CardboardView.IRenderer
{
// What is the state of the app, for tracking surface recreation inside this class.
// What is the state of the app.
enum AppState
{
Resuming, // set by android UI thread process it and transitions into 'Running' state

Paused, // set by game thread after processing 'Pausing' state
Running, // set by game thread after processing 'Resuming' state
Exited, // set by game thread after processing 'Exiting' state
Paused,
Resumed,
Exited,
}


Expand Down Expand Up @@ -79,11 +77,11 @@ public AndroidSurfaceView(Context context, AndroidGameWindow gameWindow, Game ga

public override void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
{
// Set flag to recreate gl surface or rendering can be bad on orientation change or if app
// is closed in one orientation and re-opened in another.
// Set flag to recreate gl surface or rendering can be bad on orientation change
// or if app is closed in one orientation and re-opened in another.

// can only be triggered when main loop is running, is unsafe to overwrite other states
if (_appState == AppState.Running)
if (_appState == AppState.Resumed)
_isAndroidSurfaceChanged = true;

base.SurfaceChanged(holder, format, width, height);
Expand Down Expand Up @@ -151,6 +149,8 @@ private void RunOnDrawFrame()
try { RunStep(); } // tick
catch (Exception ex) { /* ignore */ }
}

return;
}

void RunStep()
Expand All @@ -159,16 +159,11 @@ void RunStep()

switch (_appState)
{
case AppState.Resuming: // when ui thread wants to resume
processStateResuming();
break;

case AppState.Running: // when we are running game
processStateRunning();
case AppState.Resumed:
processStateResumed();
break;

case AppState.Paused: // when game thread processed pausing event
// this must be processed outside of this loop, in the new task thread!
case AppState.Paused:
break;

case AppState.Exited:
Expand All @@ -182,10 +177,9 @@ void RunStep()
return;
}

void processStateResuming()
void processStateResumed()
{
// this can happen if pause is triggered immediately after resume so that SurfaceCreated callback doesn't get called yet,
// in this case we skip the resume process and pause sets a new state.
// do not run game if surface is not available
if (_isAndroidSurfaceAvailable)
{
// create surface if context is available
Expand All @@ -194,6 +188,7 @@ void processStateResuming()
if (_eglSurface == null)
{
CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
Expand Down Expand Up @@ -238,6 +233,7 @@ void processStateResuming()
if (_eglSurface == null)
{
CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
Expand All @@ -255,18 +251,6 @@ void processStateResuming()
}
}

_isAndroidSurfaceChanged = false;

// go to next state
_appState = AppState.Running;
}
}

void processStateRunning()
{
// do not run game if surface is not available
if (_isAndroidSurfaceAvailable)
{
// needed at app start
if (_eglContext != null && _isAndroidSurfaceChanged)
{
Expand All @@ -279,10 +263,9 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
_isAndroidSurfaceChanged = false;
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}

// check if app wants to exit
Expand Down Expand Up @@ -312,7 +295,7 @@ void processStateRunning()

internal void Resume()
{
_appState = AppState.Resuming;
_appState = AppState.Resumed;

try
{
Expand All @@ -327,7 +310,6 @@ internal void Resume()

internal void Pause()
{
// prepare for next game loop iteration
_appState = AppState.Paused;
}

Expand Down
Loading