Skip to content

Commit

Permalink
Remove _glSurfaceAvailable (#1568)
Browse files Browse the repository at this point in the history
* refactor CreateGLSurface()

* replace _glSurfaceAvailable

* simplify (_eglSurface != null) checks
  • Loading branch information
nkast authored May 16, 2024
1 parent 662c648 commit bb07576
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 61 deletions.
54 changes: 20 additions & 34 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ enum AppState
volatile bool _forceRecreateSurface = false;
bool _androidSurfaceAvailable = false;

bool _glSurfaceAvailable;
bool _lostglContext;

DateTime _prevTickTime;
Expand Down Expand Up @@ -178,15 +177,11 @@ void Java.Lang.IRunnable.Run()
{
_isCancellationRequested = null;

if (_glSurfaceAvailable)
if (_eglSurface != null)
{
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
{
ClearCurrentContext();
DestroyGLSurface();
}
ClearCurrentContext();
DestroyGLSurface();
_eglSurface = null;
_glSurfaceAvailable = false;
}

if (_eglContext != null)
Expand Down Expand Up @@ -258,7 +253,7 @@ void processStateResuming()
{
try
{
if (!_glSurfaceAvailable)
if (_eglSurface == null)
CreateGLSurface();
}
catch (Exception ex)
Expand Down Expand Up @@ -298,7 +293,7 @@ void processStateResuming()

CreateGLContext();

if (!_glSurfaceAvailable)
if (_eglSurface == null)
CreateGLSurface();

// OGL.InitExtensions() must be called while we have a gl context.
Expand All @@ -317,7 +312,7 @@ void processStateResuming()
}

// finish state if surface created, may take a frame or two until the android UI thread callbacks fire
if (_glSurfaceAvailable)
if (_eglSurface != null)
{
// must resume openAL device here
Microsoft.Xna.Platform.Audio.AudioService.Resume();
Expand All @@ -334,13 +329,9 @@ void ForceSurfaceRecreation()
// needed at app start
if (_eglContext != null && _androidSurfaceAvailable)
{
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
{
ClearCurrentContext();
DestroyGLSurface();
}
ClearCurrentContext();
DestroyGLSurface();
_eglSurface = null;
_glSurfaceAvailable = false;

CreateGLSurface();

Expand Down Expand Up @@ -390,16 +381,12 @@ void processStateRunning()

void processStatePausing()
{
if (_glSurfaceAvailable)
// Surface we are using needs to go away
if (_eglSurface != null)
{
// Surface we are using needs to go away
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
{
ClearCurrentContext();
DestroyGLSurface();
}
ClearCurrentContext();
DestroyGLSurface();
_eglSurface = null;
_glSurfaceAvailable = false;
}

// must pause openAL device here
Expand Down Expand Up @@ -479,7 +466,7 @@ private void DestroyGLDisplay()

protected void DestroyGLSurface()
{
System.Diagnostics.Debug.Assert(_eglSurface != null && _eglSurface != EGL10.EglNoSurface);
System.Diagnostics.Debug.Assert(_eglSurface != null);

if (!_egl.EglDestroySurface(_eglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GetErrorAsString());
Expand Down Expand Up @@ -742,28 +729,27 @@ private string GetErrorAsString()

protected void CreateGLSurface()
{
System.Diagnostics.Debug.Assert(_glSurfaceAvailable == false);

try
{
// If there is an existing surface, destroy the old one
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
if (_eglSurface != null)
{
ClearCurrentContext();
DestroyGLSurface();
}
_eglSurface = null;
_glSurfaceAvailable = false;

_eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);
if (_eglSurface == null || _eglSurface == EGL10.EglNoSurface)

if (_eglSurface == EGL10.EglNoSurface)
_eglSurface = null;

if (_eglSurface == null)
throw new Exception("Could not create EGL window surface" + GetErrorAsString());

if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
throw new Exception("Could not make EGL current" + GetErrorAsString());

_glSurfaceAvailable = true;

// Must set viewport after creation, the viewport has correct values in it already as we call it, but
// the surface is created after the correct viewport is already applied so we must do it again.
GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy<ConcreteGame>().GraphicsDeviceManager;
Expand All @@ -777,7 +763,7 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_glSurfaceAvailable = false;
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down
44 changes: 17 additions & 27 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ enum AppState
volatile bool _forceRecreateSurface = false;
bool _androidSurfaceAvailable = false;

bool _glSurfaceAvailable;
bool _lostglContext;

DateTime _prevTickTime;
Expand Down Expand Up @@ -210,7 +209,7 @@ void processStateResuming()
{
try
{
if (!_glSurfaceAvailable)
if (_eglSurface == null)
CreateGLSurface();
}
catch (Exception ex)
Expand Down Expand Up @@ -250,7 +249,7 @@ void processStateResuming()

//CreateGLContext();

if (!_glSurfaceAvailable)
if (_eglSurface == null)
CreateGLSurface();

// OGL.InitExtensions() must be called while we have a gl context.
Expand All @@ -269,7 +268,7 @@ void processStateResuming()
}

// finish state if surface created, may take a frame or two until the android UI thread callbacks fire
if (_glSurfaceAvailable)
if (_eglSurface != null)
{
// must resume openAL device here
Microsoft.Xna.Platform.Audio.AudioService.Resume();
Expand All @@ -286,13 +285,9 @@ void ForceSurfaceRecreation()
// needed at app start
if (_eglContext != null && _androidSurfaceAvailable)
{
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
{
ClearCurrentContext();
DestroyGLSurface();
}
ClearCurrentContext();
DestroyGLSurface();
_eglSurface = null;
_glSurfaceAvailable = false;

CreateGLSurface();

Expand Down Expand Up @@ -342,16 +337,12 @@ void processStateRunning()

void processStatePausing()
{
if (_glSurfaceAvailable)
// Surface we are using needs to go away
if (_eglSurface != null)
{
// Surface we are using needs to go away
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
{
ClearCurrentContext();
DestroyGLSurface();
}
ClearCurrentContext();
DestroyGLSurface();
_eglSurface = null;
_glSurfaceAvailable = false;
}

// must pause openAL device here
Expand Down Expand Up @@ -430,7 +421,7 @@ private void DestroyGLDisplay()

protected void DestroyGLSurface()
{
System.Diagnostics.Debug.Assert(_eglSurface != null && _eglSurface != EGL10.EglNoSurface);
System.Diagnostics.Debug.Assert(_eglSurface != null);

if (!_egl.EglDestroySurface(_eglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GetErrorAsString());
Expand Down Expand Up @@ -567,30 +558,29 @@ private string GetErrorAsString()

protected void CreateGLSurface()
{
System.Diagnostics.Debug.Assert(_glSurfaceAvailable == false);

try
{
// If there is an existing surface, destroy the old one
if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface)
if (_eglSurface != null)
{
ClearCurrentContext();
DestroyGLSurface();
}
_eglSurface = null;
_glSurfaceAvailable = false;

/* Cardboard: Surface was created by GLSurfaceView.
_eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);
if (_eglSurface == null || _eglSurface == EGL10.EglNoSurface)
if (_eglSurface == EGL10.EglNoSurface)
_eglSurface = null;
if (_eglSurface == null)
throw new Exception("Could not create EGL window surface" + GetErrorAsString());
if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
throw new Exception("Could not make EGL current" + GetErrorAsString());
*/

_glSurfaceAvailable = true;

// Must set viewport after creation, the viewport has correct values in it already as we call it, but
// the surface is created after the correct viewport is already applied so we must do it again.
GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy<ConcreteGame>().GraphicsDeviceManager;
Expand All @@ -604,7 +594,7 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_glSurfaceAvailable = false;
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down

0 comments on commit bb07576

Please sign in to comment.