Code example for EGL10

Methods: eglChooseConfig, eglCreateContext, eglCreateWindowSurface, eglGetDisplay, eglGetError, eglInitialize, eglMakeCurrent

0
 
    boolean initGL() { 
        // Initialize openGL engine 
        mEgl = (EGL10) EGLContext.getEGL();
 
        mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
            Log.e(LOG_TAG, "eglGetDisplay failed " +
                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
            return false; 
        } 
 
        int[] version = new int[2];
        if (!mEgl.eglInitialize(mEglDisplay, version)) {
            Log.e(LOG_TAG, "eglInitialize failed " +
                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
            return false; 
        } 
 
        mEglConfig = chooseEglConfig();
        if (mEglConfig == null) {
            Log.e(LOG_TAG, "eglConfig not initialized.");
            return false; 
        } 
 
        mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig, EGL10.EGL_NO_CONTEXT, null);
 
        mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, mSurface, null);
 
        if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
            int error = mEgl.eglGetError();
            Log.e(LOG_TAG,"createWindowSurface returned error");
            return false; 
        } 
 
        if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
            Log.e(LOG_TAG, "eglMakeCurrent failed " +
                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
            return false; 
        } 
 
        mGL = (GL10) mEglContext.getGL();
 
        return true; 
    } 
 
    private void finishGL() { 
        if (mEgl == null || mEglDisplay == null) {
            // Nothing to free 
            return; 
        } 
        // Unbind the current surface and context from the display 
        mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
                EGL10.EGL_NO_CONTEXT);
        if (mEglContext != null) {
            mEgl.eglDestroyContext(mEglDisplay, mEglContext);
        } 
        if (mEglSurface != null) {
            mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
        } 
    } 
 
    private void checkCurrent() { 
        if (!mEglContext.equals(mEgl.eglGetCurrentContext()) ||
                !mEglSurface.equals(mEgl.eglGetCurrentSurface(EGL10.EGL_DRAW))) {
            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
                throw new RuntimeException("eglMakeCurrent failed "
                        + GLUtils.getEGLErrorString(mEgl.eglGetError()));
            } 
        } 
    } 
 
    private EGLConfig chooseEglConfig() {
        int[] configsCount = new int[1];
        EGLConfig[] configs = new EGLConfig[1];
        if (!mEgl.eglChooseConfig(mEglDisplay, sEglConfig, configs, 1, configsCount)) {
            throw new IllegalArgumentException("eglChooseConfig failed " +
                    GLUtils.getEGLErrorString(mEgl.eglGetError()));
        } else if (configsCount[0] > 0) {
            return configs[0];
        } 
        return null; 
    }