diff --git a/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs b/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs index 0c307758c67..2397d1de481 100644 --- a/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs +++ b/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs @@ -12,6 +12,7 @@ using Android.Runtime; using Microsoft.Xna.Platform.Graphics.OpenGL; using GetParamName = Microsoft.Xna.Platform.Graphics.OpenGL.GetPName; +using Javax.Microedition.Khronos.Egl; namespace Microsoft.Xna.Platform.Graphics @@ -113,8 +114,10 @@ public override GraphicsBackend Backend } OGL_DROID _ogl; + EGLDisplay _eglDisplay; internal OGL_DROID Ogl { get { return _ogl; } } + internal EGLDisplay EglDisplay { get { return _eglDisplay; } } internal ConcreteGraphicsAdapter() { @@ -123,6 +126,30 @@ internal ConcreteGraphicsAdapter() _ogl = (OGL_DROID)OGL.Current; +#if CARDBOARD + _eglDisplay = _ogl.Egl.EglGetCurrentDisplay(); +#else + _eglDisplay = _ogl.Egl.EglGetDisplay(EGL10.EglDefaultDisplay); + if (_eglDisplay == EGL10.EglNoDisplay) + throw new Exception("Could not get EGL display" + _ogl.GetEglErrorAsString()); + + int[] version = new int[2]; + if (!_ogl.Egl.EglInitialize(_eglDisplay, version)) + throw new Exception("Could not initialize EGL display" + _ogl.GetEglErrorAsString()); +#endif + } + + ~ConcreteGraphicsAdapter() + { +#if CARDBOARD +#else + if (_eglDisplay != null) + { + if (!_ogl.Egl.EglTerminate(_eglDisplay)) + throw new Exception("Could not terminate EGL connection" + _ogl.GetEglErrorAsString()); + } + _eglDisplay = null; +#endif } public override bool Platform_IsProfileSupported(GraphicsProfile graphicsProfile) diff --git a/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs b/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs index 662bd92df6d..d3fdeab67ce 100644 --- a/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs +++ b/MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsContext.Android.cs @@ -43,10 +43,8 @@ public override void BindDisposeContext() var gd = ((IPlatformGraphicsContext)this.Context).DeviceStrategy; var adapter = ((IPlatformGraphicsAdapter)gd.Adapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - AndroidGameWindow gameWindow = AndroidGameWindow.FromHandle(((IPlatformGraphicsContext)this.Context).DeviceStrategy.PresentationParameters.DeviceWindowHandle); - ISurfaceView view = gameWindow.GameView; - if (!GL.Egl.EglMakeCurrent(view.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, _glSharedContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, _glSharedContext)) throw new Exception("Could not Bind DisposeContext" + GL.GetEglErrorAsString()); } @@ -58,10 +56,8 @@ public override void UnbindDisposeContext() var gd = ((IPlatformGraphicsContext)this.Context).DeviceStrategy; var adapter = ((IPlatformGraphicsAdapter)gd.Adapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - AndroidGameWindow gameWindow = AndroidGameWindow.FromHandle(((IPlatformGraphicsContext)this.Context).DeviceStrategy.PresentationParameters.DeviceWindowHandle); - ISurfaceView view = gameWindow.GameView; - if (!GL.Egl.EglMakeCurrent(view.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) throw new Exception("Could not Unbind DisposeContext" + GL.GetEglErrorAsString()); } diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 157c684b4f3..2ea1be07a50 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -77,10 +77,10 @@ void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, global::Androi var GL = adapter.Ogl; // unbind Context and Surface - if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString()); // destroy the old _eglSurface - if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface)) Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString()); _eglSurface = null; } @@ -100,10 +100,10 @@ void ISurfaceHolderCallback.SurfaceDestroyed(ISurfaceHolder holder) var GL = adapter.Ogl; // unbind Context and Surface - if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString()); // destroy the old _eglSurface - if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface)) Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString()); _eglSurface = null; } @@ -116,7 +116,7 @@ internal void SwapBuffers() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (!GL.Egl.EglSwapBuffers(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglSwapBuffers(adapter.EglDisplay, _eglSurface)) { if (GL.Egl.EglGetError() == 0) { @@ -178,9 +178,9 @@ void Java.Lang.IRunnable.Run() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString()); - if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface)) Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString()); _eglSurface = null; } @@ -192,16 +192,10 @@ void Java.Lang.IRunnable.Run() if (_eglContext != null) { - if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext)) + if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext)) throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString()); } _eglContext = null; - if (_eglDisplay != null) - { - if (!GL.Egl.EglTerminate(_eglDisplay)) - throw new Exception("Could not terminate EGL connection" + GL.GetEglErrorAsString()); - } - _eglDisplay = null; if (_game.GraphicsDevice != null) { @@ -247,17 +241,6 @@ void ProcessStateResumed() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (_eglDisplay == null) - { - _eglDisplay = GL.Egl.EglGetDisplay(EGL10.EglDefaultDisplay); - if (_eglDisplay == EGL10.EglNoDisplay) - throw new Exception("Could not get EGL display" + GL.GetEglErrorAsString()); - - int[] version = new int[2]; - if (!GL.Egl.EglInitialize(_eglDisplay, version)) - throw new Exception("Could not initialize EGL display" + GL.GetEglErrorAsString()); - } - // Restart due to context loss bool contextLost = false; if (_isGLContextLost) @@ -266,7 +249,7 @@ void ProcessStateResumed() // objects and re-create one. if (_eglContext != null) { - if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext)) + if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext)) throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString()); } _eglContext = null; @@ -520,17 +503,17 @@ protected void CreateGLContext() int[] numConfigs = new int[1]; EGLConfig[] results = new EGLConfig[1]; - if (!GL.Egl.EglGetConfigs(_eglDisplay, null, 0, numConfigs)) + if (!GL.Egl.EglGetConfigs(adapter.EglDisplay, null, 0, numConfigs)) { throw new Exception("Could not get config count. " + GL.GetEglErrorAsString()); } EGLConfig[] eglConfigs = new EGLConfig[numConfigs[0]]; - GL.Egl.EglGetConfigs(_eglDisplay, eglConfigs, numConfigs[0], numConfigs); + GL.Egl.EglGetConfigs(adapter.EglDisplay, eglConfigs, numConfigs[0], numConfigs); Log.Verbose("AndroidGameView", "Device Supports"); foreach (EGLConfig eglConfig in eglConfigs) { - Log.Verbose("AndroidGameView", string.Format(" {0}", SurfaceConfig.FromEGLConfig(eglConfig, GL.Egl, _eglDisplay))); + Log.Verbose("AndroidGameView", string.Format(" {0}", SurfaceConfig.FromEGLConfig(eglConfig, GL.Egl, adapter.EglDisplay))); } bool found = false; @@ -538,7 +521,7 @@ protected void CreateGLContext() foreach (SurfaceConfig surfaceConfig in surfaceConfigs) { Log.Verbose("AndroidGameView", string.Format("Checking Config : {0}", surfaceConfig)); - found = GL.Egl.EglChooseConfig(_eglDisplay, surfaceConfig.ToConfigAttribs(), results, 1, numConfigs); + found = GL.Egl.EglChooseConfig(adapter.EglDisplay, surfaceConfig.ToConfigAttribs(), results, 1, numConfigs); Log.Verbose("AndroidGameView", "EglChooseConfig returned {0} and {1}", found, numConfigs[0]); if (!found || numConfigs[0] <= 0) { @@ -556,7 +539,7 @@ protected void CreateGLContext() { Log.Verbose("AndroidGameView", "Creating GLES {0} Context", ver); - _eglContext = GL.Egl.EglCreateContext(_eglDisplay, results[0], EGL10.EglNoContext, ver.GetAttributes()); + _eglContext = GL.Egl.EglCreateContext(adapter.EglDisplay, results[0], EGL10.EglNoContext, ver.GetAttributes()); if (_eglContext == null || _eglContext == EGL10.EglNoContext) { @@ -585,7 +568,7 @@ protected void CreateGLSurface() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - _eglSurface = GL.Egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null); + _eglSurface = GL.Egl.EglCreateWindowSurface(adapter.EglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null); if (_eglSurface == EGL10.EglNoSurface) _eglSurface = null; if (_eglSurface == null) @@ -605,7 +588,7 @@ private void MakeCurrentGLContext() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (!GL.Egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, _eglSurface, _eglSurface, _eglContext)) throw new Exception("Could not make EGL current" + GL.GetEglErrorAsString()); } catch (Exception ex) @@ -640,7 +623,7 @@ protected EGLSurface CreatePBufferSurface(EGLConfig config, int[] attribList) var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - EGLSurface result = GL.Egl.EglCreatePbufferSurface(_eglDisplay, config, attribList); + EGLSurface result = GL.Egl.EglCreatePbufferSurface(adapter.EglDisplay, config, attribList); if (result == EGL10.EglNoSurface) result = null; @@ -706,7 +689,6 @@ public override bool OnGenericMotionEvent(MotionEvent e) #region Properties - private EGLDisplay _eglDisplay; private GLESVersion _glesVersion; private EGLConfig _eglConfig; private EGLContext _eglContext; @@ -716,7 +698,6 @@ public override bool OnGenericMotionEvent(MotionEvent e) #region ISurfaceView - EGLDisplay ISurfaceView.EglDisplay { get { return _eglDisplay; } } GLESVersion ISurfaceView.GLesVersion { get { return _glesVersion; } } EGLConfig ISurfaceView.EglConfig { get { return _eglConfig; } } EGLContext ISurfaceView.EglContext { get { return _eglContext; } } diff --git a/MonoGame.Framework/Platform/Android/ISurfaceView.cs b/MonoGame.Framework/Platform/Android/ISurfaceView.cs index f5aabab6e33..bda501618ba 100644 --- a/MonoGame.Framework/Platform/Android/ISurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/ISurfaceView.cs @@ -12,7 +12,6 @@ interface ISurfaceView { GLESVersion GLesVersion { get; } EGLConfig EglConfig { get; } - EGLDisplay EglDisplay { get; } EGLContext EglContext { get; } } } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index c9512aed8fe..3a1977f46e3 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -81,10 +81,10 @@ public override void SurfaceChanged(ISurfaceHolder holder, global::Android.Graph var GL = adapter.Ogl; // unbind Context and Surface - if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString()); // destroy the old _eglSurface - if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface)) Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString()); _eglSurface = null; } @@ -107,10 +107,10 @@ public override void SurfaceDestroyed(ISurfaceHolder holder) var GL = adapter.Ogl; // unbind Context and Surface - if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) + if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext)) Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString()); // destroy the old _eglSurface - if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface)) + if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface)) Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString()); _eglSurface = null; } @@ -189,19 +189,6 @@ void ProcessStateResumed() var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (_eglDisplay == null) - { - /* - _eglDisplay = GL.Egl.EglGetDisplay(EGL10.EglDefaultDisplay); - if (_eglDisplay == EGL10.EglNoDisplay) - throw new Exception("Could not get EGL display" + GL.GetEglErrorAsString()); - - int[] version = new int[2]; - if (!GL.Egl.EglInitialize(_eglDisplay, version)) - throw new Exception("Could not initialize EGL display" + GL.GetEglErrorAsString()); - */ - } - // Restart due to context loss bool contextLost = false; if (_isGLContextLost) @@ -210,7 +197,7 @@ void ProcessStateResumed() // objects and re-create one. if (_eglContext != null) { - if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext)) + if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext)) throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString()); } _eglContext = null; @@ -470,7 +457,7 @@ protected EGLSurface CreatePBufferSurface(EGLConfig config, int[] attribList) var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - EGLSurface result = GL.Egl.EglCreatePbufferSurface(_eglDisplay, config, attribList); + EGLSurface result = GL.Egl.EglCreatePbufferSurface(adapter.EglDisplay, config, attribList); if (result == EGL10.EglNoSurface) result = null; @@ -536,7 +523,6 @@ public override bool OnGenericMotionEvent(MotionEvent e) #region Properties - private EGLDisplay _eglDisplay; private GLESVersion _glesVersion; private EGLConfig _eglConfig; private EGLContext _eglContext; @@ -546,7 +532,6 @@ public override bool OnGenericMotionEvent(MotionEvent e) #region ISurfaceView - EGLDisplay ISurfaceView.EglDisplay { get { return _eglDisplay; } } GLESVersion ISurfaceView.GLesVersion { get { return _glesVersion; } } EGLConfig ISurfaceView.EglConfig { get { return _eglConfig; } } EGLContext ISurfaceView.EglContext { get { return _eglContext; } } @@ -628,11 +613,6 @@ void IRenderer.OnDrawFrame(VRCardboard.HeadTransform headTransform, VRCardboard. var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete(); var GL = adapter.Ogl; - if (_eglDisplay == null) - { - _eglDisplay = GL.Egl.EglGetCurrentDisplay(); - } - if (_eglContext == null) { _eglContext = GL.Egl.EglGetCurrentContext(); diff --git a/MonoGame.Framework/Platform/Cardboard/ISurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/ISurfaceView.cs index f5aabab6e33..bda501618ba 100644 --- a/MonoGame.Framework/Platform/Cardboard/ISurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/ISurfaceView.cs @@ -12,7 +12,6 @@ interface ISurfaceView { GLESVersion GLesVersion { get; } EGLConfig EglConfig { get; } - EGLDisplay EglDisplay { get; } EGLContext EglContext { get; } } } diff --git a/MonoGame.Framework/XNA.Framework.Cardboard.csproj b/MonoGame.Framework/XNA.Framework.Cardboard.csproj index c8c669573d5..d86699c2fbb 100644 --- a/MonoGame.Framework/XNA.Framework.Cardboard.csproj +++ b/MonoGame.Framework/XNA.Framework.Cardboard.csproj @@ -18,7 +18,7 @@ {BF257928-8B5D-4371-BD03-C18DA71918C3} MonoGame.Framework Microsoft.Xna.Framework - ANDROID;GLES;STBSHARP_INTERNAL + ANDROID;CARDBOARD;GLES;STBSHARP_INTERNAL true CS0067;CS1591;CS1574;CS0419 true