From f419d37b1b29024f8ff59070eff2526be04c3e9b Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 11:34:28 +0300 Subject: [PATCH] remove Resuming state --- .../Platform/Android/AndroidSurfaceView.cs | 55 ++++++------------- .../Platform/Cardboard/AndroidSurfaceView.cs | 54 ++++++------------ 2 files changed, 35 insertions(+), 74 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 921583c4c07..6d735564eca 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -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; @@ -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; } @@ -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(); } } @@ -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: @@ -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 @@ -245,6 +236,7 @@ void processStateResuming() if (_eglSurface == null) { CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); } @@ -289,6 +281,7 @@ void processStateResuming() if (_eglSurface == null) { CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); } @@ -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) { @@ -330,10 +311,9 @@ void processStateRunning() _eglSurface = null; CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); - - _isAndroidSurfaceChanged = false; } // check if app wants to exit @@ -363,7 +343,7 @@ void processStateRunning() internal void Resume() { - _appState = AppState.Resuming; + _appState = AppState.Resumed; RequestFrame(); try @@ -379,7 +359,6 @@ internal void Resume() internal void Pause() { - // prepare for next game loop iteration _appState = AppState.Paused; } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index fabafadbf43..3eb5d45202e 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -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, } @@ -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); @@ -151,6 +149,8 @@ private void RunOnDrawFrame() try { RunStep(); } // tick catch (Exception ex) { /* ignore */ } } + + return; } void RunStep() @@ -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: @@ -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 @@ -194,6 +188,7 @@ void processStateResuming() if (_eglSurface == null) { CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); } @@ -238,6 +233,7 @@ void processStateResuming() if (_eglSurface == null) { CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); } @@ -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) { @@ -279,10 +263,9 @@ void processStateRunning() _eglSurface = null; CreateGLSurface(); + _isAndroidSurfaceChanged = false; BindGLSurfaceGLContext(); GdmResetClientBounds(); - - _isAndroidSurfaceChanged = false; } // check if app wants to exit @@ -312,7 +295,7 @@ void processStateRunning() internal void Resume() { - _appState = AppState.Resuming; + _appState = AppState.Resumed; try { @@ -327,7 +310,6 @@ internal void Resume() internal void Pause() { - // prepare for next game loop iteration _appState = AppState.Paused; }