/* Checks the OpenGL error.*/
        private static void checkEglError(String prompt, IEGL10 egl)
        {
            int error;

            while ((error = egl.EglGetError()) != EGL10.EglSuccess)
            {
                Log.Error("PikkartCore3", String.Format("%s: EGL error: 0x%x", prompt, error));
            }
        }
        public bool Swap()
        {
            bool ret = egl.EglSwapBuffers(window.Display, window.Surface);

            if (!ret)
            {
                int err = egl.EglGetError();
                switch (err)
                {
                case EGL11.EglContextLost:
                    throw EglContextLostException.GenerateException("EglSwapBuffers", egl, err);

                case EGL11.EglBadAlloc:
                    throw EglBadAllocException.GenerateException("EglSwapBuffers", egl, err);

                default:
                    throw EglException.GenerateException("EglSwapBuffers", egl, err);
                }
            }
            return(ret);
        }
 public static EglException GenerateException(string msg, IEGL10 egl)
 {
     if (egl == null)
     {
         return(new EglException(msg));
     }
     if (egl.EglGetError() == EGL10.EglSuccess)
     {
         return(new EglException(msg));
     }
     return(new EglException(String.Format("{0} failed with error {1} (0x{1:x})", msg, egl.EglGetError())));
 }
        internal AndroidAsyncGraphicsContext(AndroidGraphicsContext graphicsContext, AndroidWindow androidWindow, int versionMajor)
        {
            egl = EGLContext.EGL.JavaCast<IEGL10>();

            var pbufferAttribList = new[] 
                {
                    EGL10.EglWidth, 1,
                    EGL10.EglHeight, 1,
                    EGL10.EglNone
                };

            EGLDisplay = androidWindow.Display;
            var androidGraphicsContext = graphicsContext;
            var config = androidGraphicsContext.EGLConfig;

            var attribList = new[] 
                { 
                    EGL10.EglSurfaceType, EGL10.EglPbufferBit,
                    EGL10.EglRenderableType, 4, // (opengl es 2.0)
            
                    EGL10.EglRedSize, graphicsContext.GraphicsMode.ColorFormat.Red,
                    EGL10.EglGreenSize, graphicsContext.GraphicsMode.ColorFormat.Green,
                    EGL10.EglBlueSize, graphicsContext.GraphicsMode.ColorFormat.Blue,
                    EGL10.EglAlphaSize, graphicsContext.GraphicsMode.ColorFormat.Alpha,
            
                    EGL10.EglDepthSize, graphicsContext.GraphicsMode.Depth,
                    EGL10.EglStencilSize, graphicsContext.GraphicsMode.Stencil,
            
                    //Egl.SAMPLE_BUFFERS, samples > 0 ? 1 : 0,
                    EGL10.EglSamples, 0,
            
                    EGL10.EglNone,
                };

            // first ask the number of config available
            var numConfig = new int[1];
            if (!egl.EglChooseConfig(EGLDisplay, attribList, null, 0, numConfig))
            {
                throw new InvalidOperationException(string.Format("EglChooseConfig {0:x}", egl.EglGetError()));
            }

            // retrieve the available configs
            var configs = new EGLConfig[numConfig[0]];
            if (!egl.EglChooseConfig(EGLDisplay, attribList, configs, configs.Length, numConfig))
            {
                throw new InvalidOperationException(string.Format("EglChooseConfig {0:x}", egl.EglGetError()));
            }

            // choose the best config
            EGLConfig = ChooseConfigEGL(configs);

            // create the surface
            EGLSurface = egl.EglCreatePbufferSurface(EGLDisplay, EGLConfig, pbufferAttribList);
            if (EGLSurface == EGL10.EglNoSurface)
            {
                throw new InvalidOperationException(string.Format("EglCreatePBufferSurface {0:x}", egl.EglGetError()));
            }

            // 0x3098 is EGL_CONTEXT_CLIENT_VERSION
            var attribList3 = new[] { 0x3098, versionMajor, EGL10.EglNone };
            EGLContext = egl.EglCreateContext(EGLDisplay, config, androidGraphicsContext.EGLContext, attribList3);
            if (EGLContext == EGL10.EglNoContext)
            {
                throw new InvalidOperationException(string.Format("EglCreateContext {0:x}", egl.EglGetError()));
            }
        }
		public static EglException GenerateException (string msg, IEGL10 egl, int? error)
		{
			if (egl == null)
				return new EglException (msg);
			error = error ?? egl.EglGetError ();
			if (error == EGL10.EglSuccess)
				return new EglException (msg);
			return new EglException (String.Format ("{0} failed with error {1} (0x{1:x})", msg, error.Value));
		}
        internal AndroidAsyncGraphicsContext(AndroidGraphicsContext graphicsContext, AndroidWindow androidWindow, int versionMajor)
        {
            egl = EGLContext.EGL.JavaCast <IEGL10>();

            var pbufferAttribList = new[]
            {
                EGL10.EglWidth, 1,
                EGL10.EglHeight, 1,
                EGL10.EglNone
            };

            EGLDisplay = androidWindow.Display;
            var androidGraphicsContext = graphicsContext;
            var config = androidGraphicsContext.EGLConfig;

            var attribList = new[]
            {
                EGL10.EglSurfaceType, EGL10.EglPbufferBit,
                EGL10.EglRenderableType, 4,     // (opengl es 2.0)

                EGL10.EglRedSize, graphicsContext.GraphicsMode.ColorFormat.Red,
                EGL10.EglGreenSize, graphicsContext.GraphicsMode.ColorFormat.Green,
                EGL10.EglBlueSize, graphicsContext.GraphicsMode.ColorFormat.Blue,
                EGL10.EglAlphaSize, graphicsContext.GraphicsMode.ColorFormat.Alpha,

                EGL10.EglDepthSize, graphicsContext.GraphicsMode.Depth,
                EGL10.EglStencilSize, graphicsContext.GraphicsMode.Stencil,

                //Egl.SAMPLE_BUFFERS, samples > 0 ? 1 : 0,
                EGL10.EglSamples, 0,

                EGL10.EglNone,
            };

            // first ask the number of config available
            var numConfig = new int[1];

            if (!egl.EglChooseConfig(EGLDisplay, attribList, null, 0, numConfig))
            {
                throw new InvalidOperationException(string.Format("EglChooseConfig {0:x}", egl.EglGetError()));
            }

            // retrieve the available configs
            var configs = new EGLConfig[numConfig[0]];

            if (!egl.EglChooseConfig(EGLDisplay, attribList, configs, configs.Length, numConfig))
            {
                throw new InvalidOperationException(string.Format("EglChooseConfig {0:x}", egl.EglGetError()));
            }

            // choose the best config
            EGLConfig = ChooseConfigEGL(configs);

            // create the surface
            EGLSurface = egl.EglCreatePbufferSurface(EGLDisplay, EGLConfig, pbufferAttribList);
            if (EGLSurface == EGL10.EglNoSurface)
            {
                throw new InvalidOperationException(string.Format("EglCreatePBufferSurface {0:x}", egl.EglGetError()));
            }

            // 0x3098 is EGL_CONTEXT_CLIENT_VERSION
            var attribList3 = new[] { 0x3098, versionMajor, EGL10.EglNone };

            EGLContext = egl.EglCreateContext(EGLDisplay, config, androidGraphicsContext.EGLContext, attribList3);
            if (EGLContext == EGL10.EglNoContext)
            {
                throw new InvalidOperationException(string.Format("EglCreateContext {0:x}", egl.EglGetError()));
            }
        }
        private UserReadableEglConfig EglConfigToUserReadableEglConfig(EGLConfig eglConfig)
        {
            var surfaceType    = new int[1];
            var renderableType = new int[1];
            var redSize        = new int[1];
            var greenSize      = new int[1];
            var blueSize       = new int[1];
            var alphaSize      = new int[1];
            var depthSize      = new int[1];
            var stencilSize    = new int[1];
            var samples        = new int[1];

            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglSurfaceType, surfaceType))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglRenderableType, renderableType))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglRedSize, redSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglGreenSize, greenSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglBlueSize, blueSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglAlphaSize, alphaSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglDepthSize, depthSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglStencilSize, stencilSize))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }
            if (!egl.EglGetConfigAttrib(EGLDisplay, eglConfig, EGL10.EglSamples, samples))
            {
                throw new InvalidOperationException(string.Format("EglGetConfigAttrib {0:x}", egl.EglGetError()));
            }

            return(new UserReadableEglConfig
            {
                SurfaceType = surfaceType[0],
                RenderableType = renderableType[0],
                RedSize = redSize[0],
                GreenSize = greenSize[0],
                BlueSize = blueSize[0],
                AlphaSize = alphaSize[0],
                DepthSize = depthSize[0],
                StencilSize = stencilSize[0],
                Samples = samples[0]
            });
        }