Code example for EGL10

Methods: eglChooseConfig, eglCreateContext, eglGetDisplay, eglGetError, eglInitialize

0
 
        private EGLConfig chooseEglConfig() {
            int[] configsCount = new int[1];
            EGLConfig[] configs = new EGLConfig[1];
            int[] configSpec = getConfig();
            if (!mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, configsCount)) {
                throw new IllegalArgumentException("eglChooseConfig failed " +
                        GLUtils.getEGLErrorString(mEgl.eglGetError()));
            } else if (configsCount[0] > 0) {
                return configs[0];
            } 
            return null; 
        } 
 
        private static int[] getConfig() { 
            return new int[] { 
                    EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
                    EGL10.EGL_RED_SIZE, 8,
                    EGL10.EGL_GREEN_SIZE, 8,
                    EGL10.EGL_BLUE_SIZE, 8,
                    EGL10.EGL_ALPHA_SIZE, 8,
                    EGL10.EGL_DEPTH_SIZE, 0,
                    EGL10.EGL_STENCIL_SIZE, 0,
                    EGL10.EGL_NONE
            }; 
        } 
 
        EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) {
            int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
            return egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
        } 
 
        /** 
         * Initialize EGL for a given configuration spec. 
         */ 
        public void start() { 
            /* 
             * Get an EGL instance 
             */ 
            mEgl = (EGL10) EGLContext.getEGL();
 
            /* 
             * Get to the default display. 
             */ 
            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
 
            if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
                throw new RuntimeException("eglGetDisplay failed");
            } 
 
            /* 
             * We can now initialize EGL for that display 
             */ 
            int[] version = new int[2];
            if(!mEgl.eglInitialize(mEglDisplay, version)) {
                throw new RuntimeException("eglInitialize failed");
            } 
            mEglConfig = chooseEglConfig();
 
            /* 
            * Create an EGL context. We want to do this as rarely as we can, because an 
            * EGL context is a somewhat heavy object. 
            */ 
            mEglContext = createContext(mEgl, mEglDisplay, mEglConfig);
 
            if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
                mEglContext = null;
                throwEglException("createContext"); 
            } 
 
            mEglSurface = null;
        } 
 
        /** 
         * Create an egl surface for the current SurfaceTexture surface. If a surface 
         * already exists, destroy it before creating the new surface. 
         * 
         * @return true if the surface was created successfully. 
         */ 
        public boolean createSurface(SurfaceTexture surface) {
            /* 
             * Check preconditions. 
             */ 
            if (mEgl == null) {
                throw new RuntimeException("egl not initialized");
            } 
            if (mEglDisplay == null) {
                throw new RuntimeException("eglDisplay not initialized");
            } 
            if (mEglConfig == null) {
                throw new RuntimeException("mEglConfig not initialized");
            } 
 
            /* 
             *  The window size has changed, so we need to create a new 
             *  surface. 
             */ 
            destroySurfaceImp(); 
 
            /* 
             * Create an EGL surface we can render into. 
             */ 
            if (surface != null) {
                mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, surface, null);
            } else { 
                mEglSurface = null;
            } 
 
            if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
                int error = mEgl.eglGetError();
                if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
                    Log.e("EglHelper", "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
                } 
                return false; 
            } 
 
            /* 
             * Before we can issue GL commands, we need to make sure 
             * the context is current and bound to a surface. 
             */ 
            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
                /* 
                 * Could not make the context current, probably because the underlying 
                 * SurfaceView surface has been destroyed. 
                 */ 
                logEglErrorAsWarning("EGLHelper", "eglMakeCurrent", mEgl.eglGetError());
                return false; 
            } 
 
            return true; 
        } 
 
        /** 
         * Create a GL object for the current EGL context. 
         */ 
        public GL10 createGL() {
            return (GL10) mEglContext.getGL();
        } 
 
        /** 
         * Display the current render surface. 
         * @return the EGL error code from eglSwapBuffers. 
         */ 
        public int swap() { 
            if (! mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
                return mEgl.eglGetError();
            } 
            return EGL10.EGL_SUCCESS;
        } 
 
        public void destroySurface() { 
            destroySurfaceImp(); 
        } 
 
        private void destroySurfaceImp() { 
            if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_CONTEXT);
                mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
                mEglSurface = null;
            } 
        } 
 
        public void finish() { 
            if (mEglContext != null) {
                mEgl.eglDestroyContext(mEglDisplay, mEglContext);
                mEglContext = null;
            } 
            if (mEglDisplay != null) {
                mEgl.eglTerminate(mEglDisplay);
                mEglDisplay = null;
            } 
        } 
 
        private void throwEglException(String function) {
            throwEglException(function, mEgl.eglGetError());
        } 
 
        public static void throwEglException(String function, int error) {
            String message = formatEglError(function, error);
            throw new RuntimeException(message);