Skip to content

Commit

Permalink
refactor CreateGLSurface() (#1577)
Browse files Browse the repository at this point in the history
* split try/catch

* compact code

* extract GdmResetClientBounds()

* extract BindGLSurfaceGLContext()

* move BindGLSurfaceGLContext()

* move MakeCurrentContext()
  • Loading branch information
nkast authored May 18, 2024
1 parent 0e2594e commit f06ae28
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
2 changes: 0 additions & 2 deletions MonoGame.Framework/Platform/Android/AndroidGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ private void _activity_WindowUnfocused(object sender, EventArgs e)

private void OnTick(object sender, EventArgs args)
{
GameView.MakeCurrentContext();

if (_game != null && !ScreenReceiver.ScreenLocked)
{
((IPlatformGame)_game).GetStrategy<ConcreteGame>().OnFrameTick();
Expand Down
38 changes: 34 additions & 4 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal void SwapBuffers()
}
}

internal void MakeCurrentContext()
private void MakeCurrentContext()
{
if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
System.Diagnostics.Debug.WriteLine("Error Make Current" + GetErrorAsString());
Expand Down Expand Up @@ -243,7 +243,11 @@ void processStateResuming()
if (_eglContext != null && !_isGLContextLost)
{
if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
}

// Restart due to context loss
Expand Down Expand Up @@ -283,7 +287,11 @@ void processStateResuming()
CreateGLContext();

if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}

// OGL.InitExtensions() must be called while we have a gl context.
if (OGL_DROID.Current.Extensions == null)
Expand Down Expand Up @@ -322,6 +330,8 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}
Expand All @@ -338,6 +348,8 @@ void processStateRunning()

try
{
this.MakeCurrentContext();

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down Expand Up @@ -643,16 +655,35 @@ protected void CreateGLSurface()
try
{
_eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);

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

if (_eglSurface == null)
throw new Exception("Could not create EGL window surface" + GetErrorAsString());
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}

private void BindGLSurfaceGLContext()
{
try
{
if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
throw new Exception("Could not make EGL current" + GetErrorAsString());
}
catch (Exception ex)
{
Log.Error("AndroidGameView", ex.ToString());
}
}

private void GdmResetClientBounds()
{
try
{
// 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 @@ -666,7 +697,6 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down
2 changes: 0 additions & 2 deletions MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ private void _activity_WindowUnfocused(object sender, EventArgs e)

private void OnTick(object sender, EventArgs args)
{
GameView.MakeCurrentContext();

if (_game != null && !ScreenReceiver.ScreenLocked)
{
((IPlatformGame)_game).GetStrategy<ConcreteGame>().OnFrameTick();
Expand Down
40 changes: 36 additions & 4 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal void SwapBuffers()
// see: OnFinishFrame.
}

internal void MakeCurrentContext()
private void MakeCurrentContext()
{
// Surface & GL Context was created by GLSurfaceView.
return;
Expand Down Expand Up @@ -192,7 +192,11 @@ void processStateResuming()
if (_eglContext != null && !_isGLContextLost)
{
if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
}

// Restart due to context loss
Expand Down Expand Up @@ -232,7 +236,11 @@ void processStateResuming()
//CreateGLContext();

if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}

// OGL.InitExtensions() must be called while we have a gl context.
if (OGL_DROID.Current.Extensions == null)
Expand Down Expand Up @@ -271,6 +279,8 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}
Expand All @@ -287,6 +297,8 @@ void processStateRunning()

try
{
this.MakeCurrentContext();

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down Expand Up @@ -468,17 +480,38 @@ protected void CreateGLSurface()
{
/* Cardboard: Surface was created by GLSurfaceView.
_eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);
if (_eglSurface == EGL10.EglNoSurface)
_eglSurface = null;
if (_eglSurface == null)
throw new Exception("Could not create EGL window surface" + GetErrorAsString());
*/
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}

private void BindGLSurfaceGLContext()
{
try
{
/* Cardboard: Surface was created by GLSurfaceView.
if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
throw new Exception("Could not make EGL current" + GetErrorAsString());
*/
}
catch (Exception ex)
{
Log.Error("AndroidGameView", ex.ToString());
}
}

private void GdmResetClientBounds()
{
try
{
// 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 @@ -492,7 +525,6 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down

0 comments on commit f06ae28

Please sign in to comment.