Cocos2dx Android exception thrown on devices not supporting egl 3.0

The problem is in createContext ( Cocos2dxActiviti.java )

if the context fails to initialize egl.eglCreateContext( … ) will NOT return a NULL pointer. The return will be EGL10.EGL_NO_CONTEXT witch is a valid pointer. The application will crash later because the valid context was not created.

This is a possible fix for this issue:

public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {

            // create GL ES 3 context first,
            // if failed, then try to create GL ES 2 context

            int[] attributes = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL10.EGL_NONE };
            // attempt to create a OpenGL ES 3.0 context
            EGLContext context = egl.eglCreateContext(
                display, eglConfig, EGL10.EGL_NO_CONTEXT, attributes);

            if (context == EGL10.EGL_NO_CONTEXT) {
                attributes = new int[] {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
                context = egl.eglCreateContext(
                    display, eglConfig, EGL10.EGL_NO_CONTEXT, attributes);
            }

            if (context == EGL10.EGL_NO_CONTEXT)  {
                Log.e(DEVICE_POLICY_SERVICE, "Unable to create eagl context!");
            }

        return context;
    }

Yep, we found it. And i think we should change to

public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {

            // create GL ES 3 context first,
            // if failed, then try to create GL ES 2 context

            int[] attributes = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL10.EGL_NONE };
            // attempt to create a OpenGL ES 3.0 context
            EGLContext context = egl.eglCreateContext(
                display, eglConfig, EGL10.EGL_NO_CONTEXT, attributes);

            if (context == null || context == EGL10.EGL_NO_CONTEXT) {
                attributes = new int[] {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
                context = egl.eglCreateContext(
                    display, eglConfig, EGL10.EGL_NO_CONTEXT, attributes);
            }

        return context;
    }
1 Like

Fixed in this PR: https://github.com/cocos2d/cocos2d-x/pull/17671

Hello!

Copy / paste mistake, isn’t it?
if (context == EGL10.EGL_NO_CONTEXT || context == EGL10.EGL_NO_CONTEXT) {

}

That doesn’t make sense!

@lcd2612 yep, i changed it. You can see the PR, the PR is correct.