コード例 #1
0
        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);
            }
        }
コード例 #2
0
        public EGLSurface CreatePBufferSurface(EGLConfig config, int[] attribList)
        {
            IEGL10     egl    = EGLContext.EGL.JavaCast <IEGL10> ();
            EGLSurface result = egl.EglCreatePbufferSurface(eglDisplay, config, attribList);

            if (result == null || result == EGL10.EglNoSurface)
            {
                throw EglException.GenerateException("EglCreatePBufferSurface", egl, null);
            }
            return(result);
        }
コード例 #3
0
        public void CreateSurface(EGLConfig config)
        {
            if (refHolder == null)
            {
                CreatePBufferSurface(config);
                return;
            }

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

            eglSurface = egl.EglCreateWindowSurface(eglDisplay, config, ((Java.Lang.Object)Holder), null);
            if (eglSurface == null || eglSurface == EGL10.EglNoSurface)
            {
                throw EglException.GenerateException("EglCreateWindowSurface", egl, null);
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public void MakeCurrent(IWindowInfo win)
        {
            if (win == null)
            {
                ClearCurrent();
                return;
            }
            var w = win as AndroidWindow;

            if (w == null)
            {
                w = window;
            }
            var surf = surface == null ? w.Surface : surface;
            var ctx  = EGLContext;

            if (win == null)
            {
                surf = EGL10.EglNoSurface;
                ctx  = EGL10.EglNoContext;
            }

            if (!egl.EglMakeCurrent(window.Display, surf, surf, ctx))
            {
                int err = egl.EglGetError();
                switch (err)
                {
                case EGL11.EglContextLost:
                    throw EglContextLostException.GenerateException("MakeCurrent", egl, err);

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

                default:
                    throw EglException.GenerateException("MakeCurrent", egl, err);
                }
            }
        }
コード例 #6
0
        void Init(GraphicsMode mode, IWindowInfo win, IGraphicsContext sharedContext,
                  int major, int minor, GraphicsContextFlags flags)
        {
            window = win as AndroidWindow;
            if (window == null)
            {
                throw new ArgumentException("win");
            }

            AndroidGraphicsContext shared = sharedContext as AndroidGraphicsContext;

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

            window.InitializeDisplay();

            if (mode == null)
            {
                mode = new GraphicsMode();
            }

            if (mode is AndroidGraphicsMode)
            {
                GraphicsMode = mode;
            }
            else
            {
                GraphicsMode = new AndroidGraphicsMode(window.Display, major, mode);
            }

            if (shared != null && !PBufferSupported)
            {
                throw new EglException("Multiple Context's are not supported by this device");
            }

            if (Mode.Config == null)
            {
                Mode.Initialize(window.Display, major);
            }

            /*
             * Create an OpenGL ES context. We want to do this as rarely as possible, an
             * OpenGL context is a somewhat heavy object.
             */
            int EglContextClientVersion = 0x3098;
            int EglContextMinorVersion  = 0x30fb;

            int[] attribList = null;
            if (major >= 2)
            {
                string extensions = egl.EglQueryString(window.Display, Egl.Egl.EXTENSIONS);
                if (minor > 0 && !string.IsNullOrEmpty(extensions) && extensions.Contains("EGL_KHR_create_context"))
                {
                    attribList = new int [] { EglContextClientVersion, major,
                                              EglContextMinorVersion, minor,
                                              EGL10.EglNone };
                }
                else
                {
                    attribList = new int [] { EglContextClientVersion, major,
                                              EGL10.EglNone };
                }
            }

            EGLContext = egl.EglCreateContext(window.Display,
                                              EGLConfig,
                                              shared != null && shared.EGLContext != null ? shared.EGLContext : EGL10.EglNoContext,
                                              attribList);

            if (EGLContext == EGL10.EglNoContext)
            {
                throw EglException.GenerateException("EglCreateContext == EGL10.EglNoContext", egl, null);
            }

            if (shared != null && shared.EGLContext != null)
            {
                egl.EglMakeCurrent(window.Display, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext);
                int[] pbufferAttribList = new int [] { EGL10.EglWidth, 64, EGL10.EglHeight, 64, EGL10.EglNone };
                surface = window.CreatePBufferSurface(EGLConfig, pbufferAttribList);
                if (surface == EGL10.EglNoSurface)
                {
                    throw new EglException("Could not create PBuffer for shared context!");
                }
            }
        }
コード例 #7
0
        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
        }