void ChooseConfig (EGLDisplay display)
		{
#if LOGGING
			Log.Verbose ("AndroidGraphicsMode", "Requested graphics mode on display {7} with red {0} green {1} blue {2} alpha {3} depth {4} stencil {5} buffers {6}",
					ColorFormat.Red, ColorFormat.Green, ColorFormat.Blue, ColorFormat.Alpha, Depth, Stencil, Buffers, display);
#endif

			var egl = EGLContext.EGL.JavaCast<IEGL10> ();

			try {
				if (display == null)
					display = egl.EglGetDisplay (EGL11.EglDefaultDisplay);
			} catch {
				throw EglException.GenerateException ("Failed to get default display", egl, null);
			}

			List<int> configSpec = new List<int> {
			};

			if (ColorFormat.Red > 0) {
				configSpec.Add (EGL11.EglRedSize);
				configSpec.Add (ColorFormat.Red);
			}

			if (ColorFormat.Green > 0) {
				configSpec.Add (EGL11.EglGreenSize);
				configSpec.Add (ColorFormat.Green);
			}

			if (ColorFormat.Blue > 0) {
				configSpec.Add (EGL11.EglBlueSize);
				configSpec.Add (ColorFormat.Blue);
			}

			if (ColorFormat.Alpha > 0) {
				configSpec.Add (EGL11.EglAlphaSize);
				configSpec.Add (ColorFormat.Alpha);
			}

			if (Depth > 0) {
				configSpec.Add (EGL11.EglDepthSize);
				configSpec.Add (Depth);
			}

			if (Stencil > 0) {
				configSpec.Add (EGL11.EglStencilSize);
				configSpec.Add (Stencil);
			}
			//http://code.google.com/p/gdc2011-android-opengl/source/browse/trunk/src/com/example/gdc11/MultisampleConfigChooser.java?r=5
			if (Samples > 0) {
				// Enable Multi Sampling if we can
				configSpec.Add (EGL11.EglSampleBuffers);
				configSpec.Add (1);
				configSpec.Add (EGL11.EglSamples);
				configSpec.Add (Samples);
			}

			if (Version > 1) {
				configSpec.Add (EGL11.EglRenderableType);
				configSpec.Add (4);
			}

			configSpec.Add (EGL11.EglNone);

			int[] num_configs = new int[1];
			if (!egl.EglGetConfigs (display, null, 0, num_configs) || num_configs[0] < 1) {
				throw EglException.GenerateException ("Failed to retrieve GraphicsMode configurations", egl, null);
			}

			EGLConfig[] configs = new EGLConfig[1];
			if (!egl.EglChooseConfig (display, configSpec.ToArray(), configs, configs.Length, num_configs)) {
				Log.Warn ("AndroidGraphicsMode", "Failed to choose GraphicsMode with red {0} green {1} blue {2} alpha {3} depth {4} stencil {5} buffers {6}: egl error {7}. Falling back go lowest configuration available.",
				ColorFormat.Red, ColorFormat.Green, ColorFormat.Blue, ColorFormat.Alpha, Depth, Stencil, Buffers, egl.EglGetError ());

				configSpec = new List<int> {
					EGL11.EglRedSize, 4,
					EGL11.EglGreenSize, 4,
					EGL11.EglBlueSize, 4,
					EGL11.EglNone
				};

				if (!egl.EglChooseConfig (display, configSpec.ToArray(), configs, configs.Length, num_configs)) {
					throw EglException.GenerateException ("Failed to find a valid GraphicsMode configuration", egl, null);
				}
			}

			EGLConfig active_config = configs[0];

#if LOGGING
			Log.Verbose ("AndroidGraphicsMode", "Checking selected config {0}", active_config);
#endif

			if (active_config == null)
				throw EglException.GenerateException ("Failed to find a valid GraphicsMode configuration", egl, null);

			var r = GetAttrib (egl, display, active_config, EGL11.EglRedSize);
			var g = GetAttrib (egl, display, active_config, EGL11.EglGreenSize);
			var b = GetAttrib (egl, display, active_config, EGL11.EglBlueSize);
			var a = GetAttrib (egl, display, active_config, EGL11.EglAlphaSize);
			var depth = GetAttrib (egl, display, active_config, EGL11.EglDepthSize);
			var stencil = GetAttrib (egl, display, active_config, EGL11.EglStencilSize);
			var s = GetAttrib (egl, display, active_config, EGL11.EglSampleBuffers);
			var samples = GetAttrib (egl, display, active_config, EGL11.EglSamples);
			var bufs = GetAttrib (egl, display, active_config, EGL11.EglRenderBuffer);
			var surfaceType = GetAttrib (egl, display, active_config, EGL11.EglSurfaceType);

#if LOGGING
			Log.Verbose ("AndroidGraphicsMode", "Requested graphics mode with red {0} green {1} blue {2} alpha {3} depth {4} stencil {5} buffers {6} samples {7}",
					ColorFormat.Red, ColorFormat.Green, ColorFormat.Blue, ColorFormat.Alpha, Depth, Stencil, Buffers, Samples);
#endif

			this.Index = active_config.Handle;
			this.ColorFormat = new ColorFormat (r, g, b, a);
			this.Depth = depth;
			this.Stencil = stencil;
			this.Samples = s > 0 ? samples : 0;
			this.Config = active_config;
			this.Buffers = bufs;
			this.PBufferSupported = (surfaceType & EGL11.EglPbufferBit) == EGL11.EglPbufferBit;

#if LOGGING
			Log.Verbose ("AndroidGraphicsMode", "Selected  graphics mode with red {0} green {1} blue {2} alpha {3} depth {4} stencil {5} buffers {6}, samples {7}",
					ColorFormat.Red, ColorFormat.Green, ColorFormat.Blue, ColorFormat.Alpha, Depth, Stencil, Buffers, Samples);
#endif
		}
		/// <summary>
		///	  Creates an AndroidGraphicsMode instance, copying the values set in the mode parameter.
		///	  Passing null to the display parameter for lazy initialization is recommended.
		/// </summary>
		/// <param name="display">
		///	  A <see cref="T:Javax.Microedition.Khronos.Egl.EGLDisplay" /> with the display to use for creating the
		///	  <see cref="T:Javax.Microedition.Khronos.Egl.EGLConfig" /> that represents this graphics configuration in
		///	 the OpenGL space. Pass null for lazy initialization.
		/// </param>
		/// <param name="version">Requested OpenGL version (1 or 2)</param>
		/// <param name="mode">GraphicsMode to copy values from.</param>
		public AndroidGraphicsMode (EGLDisplay display, int version, GraphicsMode mode) :
			this (display, version, mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.Buffers, mode.Stereo)
		{
		}
		/// <summary>
		///	  Creates an AndroidGraphicsMode instance with the required settings.
		///	  Passing null to the display parameter for lazy initialization is recommended.
		/// </summary>
		/// <param name="display">
		///	  A <see cref="T:Javax.Microedition.Khronos.Egl.EGLDisplay" /> with the display to use for creating the
		///	  <see cref="T:Javax.Microedition.Khronos.Egl.EGLConfig" /> that represents this graphics configuration in
		///	 the OpenGL space. Pass null for lazy initialization.
		/// </param>
		/// <param name="version">Requested OpenGL version (1 or 2)</param>
		/// <param name="color">The ColorFormat of the color buffer.</param>
		/// <param name="depth">The number of bits in the depth buffer.</param>
		/// <param name="stencil">The number of bits in the stencil buffer.</param>
		/// <param name="samples">The number of samples for FSAA.</param>
		/// <param name="stereo">Set to true for a GraphicsMode with stereographic capabilities.</param>
		/// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
		public AndroidGraphicsMode (EGLDisplay display, int version, ColorFormat color, int depth, int stencil, int samples, int buffers, bool stereo)
		{
			this.ColorFormat = color;
			this.Depth = depth;
			this.Stencil = stencil;
			this.Samples = samples;
			this.AccumulatorFormat = color;
			this.Buffers = buffers;
			this.Stereo = stereo;
			this.Version = version;
		}
		public void Initialize (EGLDisplay display, int version)
		{
			Version = version;
			ChooseConfig (display);
		}
		int GetAttrib (IEGL10 egl, EGLDisplay display, EGLConfig config, int attrib)
		{
			int[] ret = new int [1];
			try {
				egl.EglGetConfigAttrib (display, config, attrib, ret);
			} catch (Exception e) {
				Log.Warn ("AndroidGraphicsMode", "EglGetConfigAttrib {0} threw exception {1}", attrib, e);
			}
			return ret[0];
		}
		public void TerminateDisplay ()
		{
			if (eglDisplay != null) {
				IEGL10 egl = EGLContext.EGL.JavaCast<IEGL10> ();
				if (!egl.EglTerminate (eglDisplay))
					Log.Warn ("AndroidWindow", "Failed to terminate display {0}.", eglDisplay);
				eglDisplay = null;
			}
		}
Beispiel #7
0
		/// <summary>
		/// </summary>
		/// <param name="eglDisplay"> </param>
		/// <param name="glConfig"> </param>
		/// <param name="shareList"> </param>
		/// <returns> </returns>
		public EGLCONTEXT CreateNewContext( EGLDisplay eglDisplay, EGLConfig glConfig, EGLCONTEXT shareList )
		{
			var contexAttrs = new int[] { 1, 2, EGL10Consts.EglNone };
			EGLCONTEXT context = null;
			if ( eglDisplay == null )
			{
				context = EGLCONTEXT.EGL11.EglCreateContext( this._glDisplay, glConfig, shareList, contexAttrs );
			}
			else
			{
				context = EGLCONTEXT.EGL11.EglCreateContext( this._glDisplay, glConfig, null, contexAttrs );
			}

			if ( context == null )
			{
				throw new AxiomException( "Fail to create New context" );
			}

			return context;
		}
		public void InitializeDisplay ()
		{
			if (eglDisplay != null && eglDisplay != EGL10.EglNoDisplay)
				return;

			IEGL10 egl = EGLContext.EGL.JavaCast<IEGL10> ();

			if (eglDisplay == null)
				eglDisplay = egl.EglGetDisplay (EGL10.EglDefaultDisplay);

			if (eglDisplay == EGL10.EglNoDisplay)
				throw EglException.GenerateException ("EglGetDisplay == EGL10.EglNoDisplay", egl, null);

			int[] version = new int[2];
			if (!egl.EglInitialize (eglDisplay, version)) {
				throw EglException.GenerateException ("EglInitialize", egl, null);
			}
		}
Beispiel #9
0
		/// <summary>
		/// </summary>
		/// <param name="display"> </param>
		/// <param name="win"> </param>
		/// <returns> </returns>
		protected EGLSurface CreateSurfaceFromWindow( EGLDisplay display, NativeWindowType win )
		{
			throw new NotImplementedException();
		}
        private void DestroyEGLContext()
        {
            // Assumes lockObject is locked

            ExEnLog.WriteLine("ExEnAndroidSurfaceView.DestroyEGLContext Begin");

            if(eglContext != null)
            {
                if(!egl.EglDestroyContext(eglDisplay, eglContext))
                    throw new ExEnSurfaceException(AddEGLError("Could not destroy EGL context"));
                eglContext = null;
            }
            if(eglDisplay != null)
            {
                if(!egl.EglTerminate(eglDisplay))
                    throw new ExEnSurfaceException(AddEGLError("Could not terminate EGL connection"));
                eglDisplay = null;
            }

            eglContextAvailable = false;

            ExEnLog.WriteLine("ExEnAndroidSurfaceView.DestroyEGLContext End");
        }
        private void CreateEGLContext()
        {
            ExEnLog.WriteLine("ExEnAndroidSurfaceView.CreateEGLContext Begin");

            // Assumes lockObject is locked

            lostEglContext = false;

            egl = EGLContext.EGL.JavaCast<IEGL10>();

            eglDisplay = egl.EglGetDisplay(EGL10Consts.EglDefaultDisplay);
            if(eglDisplay == EGL10Consts.EglNoDisplay)
                throw new ExEnSurfaceException("Could not get EGL display");

            int[] version = new int[2];
            if(!egl.EglInitialize(eglDisplay, version))
                throw new ExEnSurfaceException(AddEGLError("Could not initialize EGL display"));

            ExEnLog.WriteLine("EGL Version: " + version[0] + "." + version[1]);

            // TODO: allow GraphicsDeviceManager to specify a frame buffer configuration
            // TODO: test this configuration works on many devices:
            int[] configAttribs = new int[] {
                    //EGL10Consts.EglRedSize, 5,
                    //EGL10Consts.EglGreenSize, 6,
                    //EGL10Consts.EglBlueSize, 5,
                    //EGL10Consts.EglAlphaSize, 0,
                    //EGL10Consts.EglDepthSize, 4,
                    //EGL10Consts.EglStencilSize, 0,
                    EGL10Consts.EglNone };
            EGLConfig[] configs = new EGLConfig[1];
            int[] numConfigs = new int[1];
            if(!egl.EglChooseConfig(eglDisplay, configAttribs, configs, 1, numConfigs))
                throw new ExEnSurfaceException(AddEGLError("Could not get EGL config"));
            if(numConfigs[0] == 0)
                throw new ExEnSurfaceException("No valid EGL configs found");
            eglConfig = configs[0];

            const int EglContextClientVersion = 0x3098;
            int[] contextAttribs = new int[] { EglContextClientVersion, 1, EGL10Consts.EglNone };
            eglContext = egl.EglCreateContext(eglDisplay, eglConfig, EGL10Consts.EglNoContext, contextAttribs);
            if(eglContext == null || eglContext == EGL10Consts.EglNoContext)
            {
                eglContext = null;
                throw new ExEnSurfaceException(AddEGLError("Could not create EGL context"));
            }

            eglContextAvailable = true;

            ExEnLog.WriteLine("ExEnAndroidSurfaceView.CreateEGLContext End");
        }
Beispiel #12
0
        public Javax.Microedition.Khronos.Egl.EGLConfig ChooseConfig(Javax.Microedition.Khronos.Egl.IEGL10 egl, Javax.Microedition.Khronos.Egl.EGLDisplay eglDisplay)
        {
            Carto.Utils.Log.Debug("ConfigChooser.ChooseConfig: Model: " + Android.OS.Build.Model + ", board: " + Android.OS.Build.Board + ", product: " + Android.OS.Build.Product);

            for (int i = 0; i < ATTRIBUTE_TABLE.Length; i++)
            {
                int[]       numConfigs = new int[] { 0 };
                EGLConfig[] configs    = new EGLConfig[1];
                if (egl.EglChooseConfig(eglDisplay, ATTRIBUTE_TABLE[i], configs, 1, numConfigs))
                {
                    if (numConfigs[0] > 0)
                    {
                        Carto.Utils.Log.Debug("ConfigChooser.ChooseConfig: Selected display configuration: " + i);
                        return(configs[0]);
                    }
                }
            }
            return(null);
        }